0

In my UIViewController which is pushed in UINavigationController I am saving large amount of data in DB. I want to perform DB work asynchronously so if user tap the back button and my UIViewController pop away my DB work has no effect of this. How I can achieve this?

S.J
  • 3,063
  • 3
  • 33
  • 66

2 Answers2

0

Use Below code on your viewWillDisappear.

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),  ^{
    // do your Database Update This will not block your code
    // Make sure this method has no access to view controllers local properties. Pass data as Parameter 
    [self doUpdateDatabaseInBackground:(NSData*) dataToUpdate];
});
Meet Doshi
  • 4,241
  • 10
  • 40
  • 81
ValayPatel
  • 1,094
  • 12
  • 15
-2

You can add your database code in app delegate by creating methods in that. Whenever you want to do any operation in database you can call the method of app delegate by creating its objects.

Dipen
  • 268
  • 1
  • 14
  • And this magically makes it work in the background? Also why pollute the AppDelegate with unrelated code, that is better packaged in its own class? – trojanfoe Feb 11 '16 at 10:39
  • He doesn't want to do any operation when app is in background man. He just want to do the db operation continues while navigation controller doing it work of pushing and popping. And if you don't want to do the code in app delegate then you can do it singleton class. – Dipen Feb 11 '16 at 10:45
  • And how will the database operation continue, while the navigation controller is doing it's work, if you *don't* run it in the background? – trojanfoe Feb 11 '16 at 10:47
  • Yes, and that is the crux of the question. – trojanfoe Feb 11 '16 at 10:51