0
我試圖在用戶鎖定iPhone時將一些數據保存到Ream數據庫。您的數據可能是由背景位置更新產生的位置座標。如果領域不能這樣做。核心數據可以做到嗎?當用戶鎖定iPhone時,Will Realm是否仍然可以保存數據?
這是我如何做到這一點的代碼:
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
NSLog(@"didUpdateLocations: %@", locations);
if (locations.count > 0) {
CLLocation *location = locations.firstObject;
if (location.horizontalAccuracy < 0) {
return;
}
JBLocation *locationObject = [[JBLocation alloc] init];
locationObject.lat = location.coordinate.latitude;
locationObject.lon = location.coordinate.longitude;
locationObject.date = [NSDate new];
locationObject.speed = location.speed;
RLMRealm *realm = [RLMRealm defaultRealm];
[realm beginWriteTransaction];
[realm addObject:locationObject];
[realm commitWriteTransaction];
}
}