Coredata非常慢;迅速 [英] Coredata is very slow; swift

查看:82
本文介绍了Coredata非常慢;迅速的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个iOS应用,用于跟踪设备的GPS路线。
问题是,当我跟踪5分钟以上时,coredata需要保存很长时间。
我保存了一个称为session的对象,session具有很多位置对象。
位置对象为[[纬度,经度]]

i've made an iOS App tracking the gps route of the device. The problem is, that coredata needs very long to save, when i am tracking more than 5 min. I save an object called session and session have much location objects. the location object is [ [latitude, longitude] ]

,会话对象看起来像[名称:字符串,日期:nsdate,平均速度:double,高速:加倍,行驶公里数:加倍,位置:[[加倍,加倍]]]

and the session object looks like [name: string, date: nsdate, average speed: double, high speed: double, driven kilometers: double, locations: [ [double, double] ] ]

因此...有效,但是5分钟后。我认为保存起来需要2到3分钟

so... it works, but after 5 min. it takes i think 2 or 3 minutes to save

推荐答案

如果您有许多相同类型的对象,那么Core Data自然会需要很长时间来保存东西。不管您做什么,都需要一段时间。您可以做的是将保存配置为在后台线程中进行,以免冻结UI。

If you have many objects of the same type, Core Data is naturally going to take a long time to save things. Doesn't matter what you do, it will take a while. What you can do is to configure the save to take places in a background thread as to not freeze your UI.

最简单的方法是为主对象创建背景上下文上下文。这个想法是,您的主上下文将其数据保存到父上下文,而父上下文负责将数据持久保存到磁盘。基本上是这样的:

The easiest way is to create a background context for your main context. The idea is that your main context saves its data to the parent context, and the parent context is in charge of persisting data to disk. Basically like this:

因为两个主上下文和父上下文在内存中运行,从子到父的保存操作很快。父对象拥有您的位置对象后,它将保存在后台线程中。它可能仍会花费很长时间,但至少它不应该冻结您的UI。

Because both your main context and parent context are running in memory, the save operation from the child to the parent is fast. Once the parent has your location objects, it will save in a background thread. It may still take a long time, but at the very least it should not freeze your UI.

您可以在代码中像这样配置它:

And you can configure this in your code like this:

lazy var parentContext: NSManagedObjectContext = {
    let moc = NSManagedObjectContext(concurrencyType:.PrivateQueueConcurrencyType)
    moc.persistentStoreCoordinator = self.coordinator
    return moc
}()

lazy var context: NSManagedObjectContext = {
    let moc = NSManagedObjectContext(concurrencyType:.MainQueueConcurrencyType)
    // moc.persistentStoreCoordinator = self.coordinator
    moc.parentContext = self.parentContext
    return moc
}()

context 将成为子上下文。您可以看到给它一个父上下文很容易。

context will be the child context. You can see how easy it is to give it a parent context.

然后,保存数据:

class func save(moc:NSManagedObjectContext) {

    moc.performBlockAndWait {

        if moc.hasChanges {

            do {
                try moc.save()
            } catch {
                print("ERROR saving context \(moc.description) - \(error)")
            }
        }

        if let parentContext = moc.parentContext {
            save(parentContext)
        }
    }
}

(代码摘自Tim撰写的通过Swift学习iOS的核心数据:动手指南一书Roadley)。

(Code taken and slightly edited from the "Learning Core Data for iOS with Swift: A Hands-on Guide" book by Tim Roadley).

这篇关于Coredata非常慢;迅速的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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