Reputation: 2535
I have an app which sync its contacts from address book, i just need to check for new contacts and sync this contact into my app database every midnight automatically.
I have gone through iOS7 new feature "Background Fetch", but this does not match my requirements, in this the background fetch will be triggered by OS depending on users activity.
However my Data gets obsolete by 12 am and if this activity is not started at this particular time instance my app will be rendered useless.
Is there any way out to achieve what I need so that I can sync my database with address book every midnight using a background process.
Thanks
Upvotes: 3
Views: 2958
Reputation: 735
James's answer is most correct. If you set up your backend to send push notifications to all your devices at midnight, you can accomplish triggering a background fetch at exactly that time. That way the user will most likely not see a loading dialog. Just don't assume with 100% certainty that the user received the push and fetched properly, since something could have gone wrong along the way.
You can set up a script to do the push, and then have a cron job call that script. I've used https://www.setcronjob.com in the past for things like this and it's worked very nicely.
Setting up a service to handle the push notification management might also be a good idea. If you choose to go that route, I recommend Parse (http://www.parse.com).
Another, simpler, option might be to just set the background fetch interval on the app to the minimum.
[[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];
In most cases, this should be sufficient for your purposes.
Upvotes: 0
Reputation:
You can subscribe to ABAddressBookRegisterExternalChangeCallback
to receive notifications when the Address Book database is modified. I think it is better than sheduled synchronization, nevertheless this will work only when the app is in the foreground.
BUT when the app is suspended it cannot do ANYTHING to rouse itself directly. It cannot previously have scheduled an NSTimer
, it cannot make use of something like performSelector:afterDelay
and so on and so forth. The ONLY way the app can become active again is if the user does something to make it active e.g local or remote notification received and an alert poped up. Execution of an app in background mode for a long time is only allowed when an app has a background mode, theses modes are: voip
, audio
, location
, newstand
. Even if it has one of these types an application cannot execute its code without some restrictions.
So the only way is iOS 7 feature: background task execution if content avaliable
as was suggested by James Frost but for this you need your external server which will send push notifications to your application.
You said: "...is not started at this particular time instance my app will be rendered useless."
but who will check this until the app will be launched again? Why can't you make all your critical updates with address book during every application run if the delta between time values is greater then some value. I mean that you can (when the app returns to the foreground in applicationDidFinishLaunching
) calculate the delta between [NSDate now] and your last synchronisation date value which you store in NSUserDefaults
or plist
after synchronisation completes. If the delta is greater e.g. than 24 hours then invoke syncronization mehtod again.
Upvotes: 1
Reputation: 2047
Do you really need to update your data exactly at midnight? Or does it have to be updated after the midnight?
You could still use the background fetch feature. Just let the OS wake you up for a check, and if it is past midnight, perform your update. Even if the OS won’t do it until the next launch of the app, you’ll do it on the next launch. If the user doesn’t launch the app in this period of time, is it really necessary to update the data at some specific time?
Upvotes: 1
Reputation: 6990
You cannot configure the time at which a Background Fetch will take place. Instead, you probably want to take a look at the new Remote Notification feature of iOS 7.
Remote notifications are essentially push notifications, but they include the content-available
attribute. When one of these notifications arrives, the system will wake up your app and call the app delegate method:
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
Within this method, you can perform a background fetch to retrieve new data.
You'd have to set up your server to send out remote notifications to your apps once your data has been refreshed, to prompt them to update themselves.
See this issue of objc.io and 'Using Push Notifications to Initiate a Download' for more information on remote notifications.
Upvotes: 3