UICollectionView: NSInternalInconsistencyException attempt to delete section 0, but there are only 0 sections before the update in Realm notification - uicollectionview

This crash only occurs when collectionView is empty before insertion or will be empty after deletion and only happen when insertion and deletion occur in view controller other than where collectionView is and as soon as insertion deletion happen i pop the controller and collectionView controller is visible.
So here is the scenario.
I have one section in collectionView let it be Controller1.
I push from Controller1 to another controller let it be Controller2.
At Controller2 i delete the object from realm and pop the viewController.
Now realm fires notification to Controller1 which tries to delete section 0 but gets NSInternalInconsistencyException.
I cross check viewWillAppear and viewDidAppear if i was reloading data but there isn't anything.
So basically if collectionView gets empty it automatically reload.
Does anyone has an idea why this happen?
Ok this only happens on physical devices and not on simulator. Now i am very curious to know why this actually happens.

Related

BarButtonItem change after segue is visible

I have an app with a navigation controller that does a segue to another view controller and then checks for the presence of an object in viewDidLoad (that may or may not exist). It then updates the name of a barButtonItem if the object exists. The problem is I can see the button expanding to accommodate the new name as the segue occurs.
How can I make this button change before the view is loaded so this jumping button effect is not seen? Shouldn't changes made in viewDidLoad not be seen until after viewDidAppear?

UITableViewCell<MFMailComposeViewControllerDelegate> crash didReceiveMemoryWarning

I have a custom UITableViewCell that extends UITableViewCell and implements MFMailComposeViewControllerDelegate. (i.e. UITableViewCell<MFMailComposeViewControllerDelegate>).
When a button is clicked in the custom table cell, I present a mailController with presentModalViewController. The user can then type the email or cancel, everything works.
But when didReceiveMemoryWarning is called while the mailController is present, and then when the mailController closes, the app crashes. I get this error:
"-[CustomCell respondsToSelector:]: message sent to deallocated instance 0xf4988b0"
Now I'm pretty sure its because the table view that owns the tablecell has been deallocated hence the cell has been deallocated but does anyone have any suggestions to go about fixing this? I mean I guess I could switch the MailCompose delegate to the table cell's table view's view controller but I'd rather not. I'd rather keep it in the table cell. Any ideas?
You should not be using a table view cell as the MFMailComposeViewControllerDelegate. Because of the nature of cells and their reuse, it's hard to pin a particular instance to be a delegate, especially a delegate of a view controller on top of the view controller the cell is being shown, and on this particular case, the memory warning might be causing a flush of the cache of cells.
The MFMailComposeViewControllerDelegate should be the delegate of the table view where the cell is. It's also a better MVC pattern.

viewWillAppear getting called before viewDidDisappear of another controller gets finished

I have 2 view controllers say VC-1, VC-2. I have VC-2 displayed over VC-1 through navigation stack. VC-1 has textView and VC-2 has webView. Now I need to update the VC-1's content as soon as VC-2 is popped off. Hence I am using viewDidDisappear method of VC-2 to set the content and using that in viewWillAppear of VC-1 to update the textView (VC-1's content). But before viewDidDisappear finishes itself viewWillAppear of VC-1 gets executed and the content is not updated. I have checked this by NSLogging. viewDidDisappear has some file reading stuff so I guess that is causing the problem. How can I make the WillAppear(of VC-1) to get called after DidDisappear(VC-2) gets finished?
-viewWillAppear: is called before your view comes on screen at all. -viewDidDisappear: is called after your view is wholly gone from the screen. Therefore on any sort of animated transition, e.g. a navigation stack pop/push, the incoming VC's -viewWillAppear: will necessarily be called before the departing VC's -viewDidDisappear: is called. This is perfectly correct.
You should probably take the work you're doing in -viewDidDisappear: and move that to -viewWillDisappear: instead.

Objective-C – reloading a UITableView without noticing the user

How can I reload a UITableView without noticing the user behind the iDevice?
I'm updating my UITableViews datasource in -applicationWillEnterForeground: by sending a notification to my RootViewController that observes the notification and runs a selector that calls [self.table reloadData] but I want this update to be made in stealth and not showed to the user.
It seems that -viewWillAppear: is fired before -applicationWillEnterForeground:
I'm a little confused by your question, but I think what you're looking for is a way to reload the data before it appears on screen, yes?
I'd say figure out a way to call your data refresh method directly, not via a notification. The notification de-couples the data refresh from -applicationWillEnterForeground, giving the view time to appear.
Sequence is probably something like this:
applicationWillEnterForeground
viewWillAppear
The view appears.
Notification observer method is called
a) Notification observer method calls [self.table reloadData]
Table view refreshes on-screen.
What you want is to couple your notification method to a method that gets called before the view appears. Call it directly from applicationWillEnterForeground or viewWillAppear.
That way it'll play out like this:
applicationWillEnterForeground
a) your data refresh method is called. (no need to call [self.table reloadData], the tableView hasn't loaded the data at all yet.)
viewWillAppear
The view appears.
Table view is loaded with already refreshed data.
or like this:
applicationWillEnterForeground
viewWillAppear
a) your data refresh method is called. (no need to call [self.table reloadData], the tableView hasn't loaded the data at all yet.)
The view appears.
Table view is loaded with already refreshed data.

popViewController problem in tableViewController

In my app delegate I have a navigationController property.
In my first view I have some buttons and tapping them will make another view appear when it is pushed on navController.
In this new view there is another button to open a UITableViewController by pushing it on the navController.
The problem is in the last view, UITableViewController, in fact in viewDidLoad, if I have no data, I try to pop it off the navigationController but my app crashes.
However, if I connect the pop to a button it works great.
I reference my app delegate instance in order to popViewControllerAnimated:, so what is the problem?
I'm not sure what's wrong with the code, can you post your viewDidLoad method?
Also, is it possible to check whether your table will have data BEFORE you push the tableView onto the nav stack? That would be a much cleaner UI, rather than showing and then immediately popping a view. If there is no data, disable the button that launches the table view.