I want my app in objective-c to delete the core data and then reload it in the background, so the user can still use the current core data. I want this to happen every 30 seconds then have it update on screen, is this possible?
Asked
Active
Viewed 157 times
0
-
should this be done in background when user just changes apps or even when user quits apps? – Ricardo Alves Jun 08 '17 at 15:19
-
While the user is still using the app. I have it connected to a mysql database, so I want it to pull new information every 30 seconds in the background then update it on the screen. So the user can still use the app while this happens. – King Jun 08 '17 at 15:26
1 Answers
1
You can accomplish this by having a multithread that makes the call to the api and updates core data while running the app
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
//Background Thread
dispatch_async(dispatch_get_main_queue(), ^(void){
//Run UI Updates
});
});
Inside the dispatch_async you can have it call himself with a timer, that way, when finished updating the database, it will wait for more 30 seconds until the next call
[self performSelector:@selector(YourFunctionName)
withObject:(can be Self or Object from other Classes)
afterDelay:(Time Of Delay)];
Now, be careful as this can create some memory management issues

Ricardo Alves
- 642
- 2
- 13
- 34
-
Can I put the code for the new thread anywhere, or is there a certain way to implement it? – King Jun 08 '17 at 15:39
-
1app delegate would be a good idea, that way you are sure that it will always be run, or on you main ViewController. which one you think is better – Ricardo Alves Jun 08 '17 at 15:41
-
Also, when I dispatch to the main thread will it interfere with what the user is currently doing (cause lag)? – King Jun 08 '17 at 15:43
-
1you should create a method on appDelegate and call that method on applicationDidBecomeActive, that way you can call it again – Ricardo Alves Jun 08 '17 at 15:46
-
How do I make the changes appear on the main screen? cant get this to work – King Jun 08 '17 at 16:24
-
try using NSNotificationCenter, that way you can update on the main screen – Ricardo Alves Jun 08 '17 at 16:53
-
https://stackoverflow.com/questions/44460326/ios-error-when-using-threads-and-core-data-objective-c, get the following error – King Jun 09 '17 at 15:18