Dan*_*Dan 8 objective-c background-process ios firebase ios7
我在iOS 7上的Objective C应用程序在后台从startUpdatingsignificantLocationChanges或startUpdatingLocation委托获取位置更新(哪一个取决于应用程序所处的模式,但我认为不重要).
在委托中,我收集位置信息,将其写入字典,然后将字典写入Firebase.
// this code is in the location update delegate routine
// the code that gathers the various elements that go into the dictionary
// are omitted for clarity, I don't think that they matter
// we may be running in the background on iOS 7 when we are called!
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
[[NSNumber numberWithFloat:newLocation.coordinate.latitude] stringValue], @"Latitude",
[[NSNumber numberWithFloat:newLocation.coordinate.longitude] stringValue], @"Longitude",
[[NSNumber numberWithFloat:newLocation.horizontalAccuracy] stringValue], @"Accuracy",
formattedDateString, @"TimeNow",
[dateFormatter stringFromDate:newLocation.timestamp], @"TimeStamp",
[[NSNumber numberWithDouble:interval] stringValue], @"Date",
self.mode, @"Mode",
nil];
// Write it once to CurrentLocation
[ref setValue:dictionary];
// yes, I know this is clumsy
fbTmp = [NSMutableString stringWithString: fbRoot];
[fbTmp appendString : @"/locationHistory"];
ref = [[Firebase alloc] initWithUrl:fbTmp];
// Now write it again to the locationHistory list
ref = [ref childByAutoId];
[ref setValue:dictionary];
Run Code Online (Sandbox Code Playgroud)
有时它可以工作,有时它不工作(即在应用程序的同一个运行中,有时位置会按预期成功写入Firebase,有时它不会.没有任何明显的押韵或理由何时似乎工作,什么时候不工作).
我怀疑问题是Firebase写入没有在后台模式下成功完成,但我不确定.我是iOS和Objective C和Firebase的新手.
我的应用程序在其功能中标记为要求位置更新和后台获取的后台服务(后者我随机尝试解决此问题,前者我知道我需要).
我的怀疑是我需要告诉操作系统我需要时间来完成使用backkgroundTask的写入,然后在firebase写入的完成块中终止后台任务 - 有人确认在后台模式下运行时会有效吗?
如果是这样,我是否只需要在第二次写入(假设它们按顺序完成)或两者中都这样做(使用一个计数器,我在每次写入完成时倒数)?
任何提示最受赞赏.
是的,您需要在后台完成任务。苹果文档中是这么说的:
如果您的应用程序正在执行任务并且需要一些额外的时间来完成该任务,它可以调用 UIApplication 对象的 beginBackgroundTaskWithName:expirationHandler: 或 beginBackgroundTaskWithExpirationHandler: 方法来请求一些额外的执行时间。调用这些方法中的任何一个都会暂时延迟应用程序的暂停,从而给它一些额外的时间来完成其工作。完成该工作后,您的应用程序必须调用 endBackgroundTask: 方法,让系统知道它已完成并可以暂停。
只需将您的代码放入后台任务中,给它最大时间(我认为是 3 分钟)。
阅读有关Apple 应用程序生命周期的内容,一切都会为您清除,以供将来参考。
归档时间: |
|
查看次数: |
851 次 |
最近记录: |