简体   繁体   中英

App crash after hammering button

In my app the user has to press a button to generate data which is created and processed in a dedicated thread and afterwards stored via Core Data. The button is immediately being disabled after it's pressed and is only again enabled after the data was saved via Core Data.

However if I click the button repeatedly quickly it works normally for a while but after a short time the app either crashes with EXC_BAD_ACCESS (code=1, address=0x0) or I get the warning that Core data couldn't be saved so I'm wondering what could cause these issues?

The structure outline would look like this (pseudo code):

  1. User clicks button
  2. notificationCenter.addObserver("didFinishGenerationOperation")
  3. NSOperationQueue.addOperation(dataGenerationOperation)
  4. dataGenerationOperation.completionBlock { dispatch_async(dispatch_get_main_queue() { notificationCenter.postNotificationName("didFinishGenerationOperation") } }
  5. calls didFinishNameGenerationOperation() which does the following:
    1. processingQueue.waitUntilAllOperationsAreFinished()
    2. clear core data-managed array controller.
    3. add all newly generated objects to the array controller.
    4. notificationCenter.addObserver("didSaveNotification")
    5. coreData.saveData()
  6. After the save is done, calls didSaveNotification() which broadcasts notification so that GUI can be enabled again:
    1. notificationCenter.postNotificationName("generationComplete")
  7. View class receives notification and enables the button again.

This is basically the flow of the operation after the button is clicked. Notification observers are immediately being removed again anywhere after they aren't needed anymore but I didn't include that above. I don't see what could be wrong. At first look the code appears to be proof but obviously there's something going awry somewhere. Shouldn't core data be finished with saving after it dispatches the NSManagedObjectContextDidSaveNotification ? And likewise with the whole generation code that is executed on its own thread.

This app is for being released on the app store so I don't want to leave such bugs in it. Does somebody have any idea what could be causing the crashes?

For what it's worth...

I managed to fix the issue. It was due to the complexity of my code that I didn't see that one command used to clear the array controller for the generated data interfered with the part where the new data is being added. There was some discrepancy with that. So now I restructured and optimized the whole code related to the data generation and concurrent operation and I couldn't reproduce the bug anymore no matter how fast I hammer the button now. The AC is now cleared exactly once before the new data is added to it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM