Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Ryan Stevenson
1,799 PointsParse not updating when app is sent to background / minimised
I have tried entering my code in both routines below but neither write to the server when the App is sent to the background (minimised).
Please can someone help so that my App will update the server once the app has been closed?
I've also tried [post save]; and [post saveInBackground];
Thanks, Ryann
-
(void)applicationDidEnterBackground:(UIApplication *)application { NSLog(@"ENTERED BACKGROUND");
PFQuery *query = [PFUser query]; [query whereKey:@"objectId" equalTo:@"4Ljx4FjfBw"]; [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { NSArray *postArray = objects; PFObject *post = postArray.lastObject; NSLog(@"Last Object: %@", post); [post setObject:@"offline" forKey:@"onlineStatus"]; [post saveInBackground]; NSLog(@"User logged as offline."); }];
}
-
(void)applicationWillResignActive:(UIApplication *)application {
NSLog(@"ENTERED BACKGROUND");
PFQuery *query = [PFUser query]; [query whereKey:@"objectId" equalTo:@"4Ljx4FjfBw"]; [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { NSArray *postArray = objects; PFObject *post = postArray.lastObject; NSLog(@"Last Object: %@", post); [post setObject:@"offline" forKey:@"onlineStatus"]; [post saveInBackground]; NSLog(@"User logged as offline."); }]; }
1 Answer
Dennis Walsh
5,793 PointsRyan,
I think your issue is with trying to use the background processes to make these updates. If you change them to be done on the main thread do they actually update? I suspect that the background threads are not being execute because the app has already enterd the background before they get a chance.
The method applicationWillResignActive executes each line of code before the app enters the background. However, once the findObjectsInBackgroundWithBlock method is called the method is essentially complete.
The method applicationDidEnterBackground only gives you 5 seconds I believe to complete a task. If it does not finish by then you will not get the desired results. Review Apple's documentation on this method they mention shifting task to other queues.
In general app in the background are not active and do not cosume system resources. There are exceptions where apps can register for background refreshing to perform certain tasks such as updating the users location. You should review Apple's documentation on multitasking.