用户锁定iPhone后,Realm仍然能够保存数据吗? [英] Will Realm still be able to save the data while user has locked iPhone?

查看:114
本文介绍了用户锁定iPhone后,Realm仍然能够保存数据吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在用户锁定iPhone时将一些数据保存到Ream数据库.该数据可以是由背景位置更新产生的位置坐标.如果境界做不到.核心数据可以做到吗?

I am trying to save some data to Ream database while users have locked their iPhone. Thie data may be the location coordinate which produced by background location updates. If realm can't do it. Could Core Data do it?

这是我在代码中的操作方式:

This is how I do it in code:

- (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];
    }
}

推荐答案

在手机处于锁定状态时执行某些操作是后台任务的一个示例.尽管我不确定您是否会成功在后台执行一次任务,但是可以确定的是,当操作系统终止您的应用程序时,它将无法再执行您想做的事情.

Doing something while the phone is locked is an example of a background task. Though I'm not sure if you will be successful in doing a task in the background even for once, but it is certain that when the OS kills your app, it can't do what you want to do any longer.

我正在做一个与您的项目相似的项目(后台位置获取,将数据保存到本地数据库,在屏幕锁定时将其发布到服务器.)它已经完成,但是正在等待释放信号.它也已作为Beta提交(批准)给了Apple.

I'm doing in a project that is similar to yours (background location fetching, saving to local database the data, posting to the server while the screen is locked.) It is already complete but waiting for release signal. It was also submitted (approved) to the Apple as Beta.

我将此示例项目用作库并移植到Swift:

I used this sample project as a library and ported to Swift:

https://github.com/voyage11/Location

这篇关于用户锁定iPhone后,Realm仍然能够保存数据吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆