简体   繁体   中英

Core Data misses to save context with region monitoring when app is backgrounded

I have a rather strange behaviour in an app that is monitoring some CLCircularRegion objects.

What the app is expected to do

Whenever a region is entered or left, an entry in a quite simple UIManagedDocument (not located in the iCloud) will be updated or created. Furthermore, a NSLocalNotification will be fired telling me which region has been affected and how long the user stayd within the region.

What really happens

The Notifications are fired as expected, so the delegate methods are fired. Even the duration is calculated correctly. BUT, whenever the app was in background for a longer time - what means about an hour or more - there are no objects updated or created in the UIManagedDocument. This behaviour is hard to debug, because I cannot reproduce it with the debugger attached or in Simulator, just on my iPhone 5S running iOS 8.1.3.

What I already tried out

Even if the UIManagedDocument handles context saving by itself, I tried to call the context saving directly from the location managers delegate method. Without any success. Turning on the location update background capability lead the app to be rejected from the app store, because there is no need to do background location updates when working with region monitoring.

Question

Does anyone out there face similar problems or knows how to do the trick?

thank you in advance, Peter

Are you aware that the iOS app gets awoken just for a couple of seconds after entering a region in background ? After that it stops execution and gets suspended.

Maybe you should try putting your UIManagedDocument operation in a backgroundTask. Please read Executing Finite-Length Tasks in:

https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM