简体   繁体   English

在设备上存储和更新数据的最佳方法

[英]best way to store and update Data on device

Every time, when user launches application, app sends request to API and receive Data from it. 每当用户启动应用程序时,应用程序都会向API发送请求并从中接收Data I have to storage old data in a file on device and update them with every launch. 我必须将旧数据存储在设备上的文件中,并在每次启动时进行更新。 When size of Data will be over than 3-5 mb, reading can be very long, isn't it? 当数据大小超过3-5 mb时,读取时间可能会很长,不是吗?
My questions is: if i will store more than 5 mb of Data in file, can i quickly fetchs them, or only use database? 我的问题是:如果我要在文件中存储超过5 mb的数据,我可以快速获取它们,还是仅使用数据库?
If yes, how can i create directory and put file to it? 如果是,我如何创建目录并将文件放入其中?
And should i archive them? 我应该存档吗?

I had a similar task and used CoreData with background fetch via DispatchQueue. 我有一个类似的任务,并通过DispatchQueue将CoreData与后台获取一起使用。 I think this should help. 我认为这应该有所帮助。

EDIT: 编辑:

guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
        return
}
DispatchQueue.global(qos: .background).async {

    let managedContext = appDelegate.persistentContainer.viewContext
    let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "ENTITY")
    do {
        self."NSManagedObject ARRAY" = try managedContext.fetch(fetchRequest)
        } catch let error as NSError {
            print("Could not fetch. \(error), \(error.userInfo)")
          }
    DispatchQueue.main.async {
        //MAKING CHANGES TO VIEW
    }
}

PS I don't know how fast this fetch is, though it helped a lot in my task. PS我不知道这种提取有多快,尽管它对我的任务很有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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