简体   繁体   English

Swift-使用CoreData从数据库预加载和更新数据

[英]Swift - Preload and update data from database with CoreData

I have a large database containing products. 我有一个包含产品的大型数据库。 I was thinking of fetching all products from the database when the iOS app launches and then save and restructure the data with CoreData, are you following? 我当时想在启动iOS应用程序时从数据库中获取所有产品,然后使用CoreData保存和重组数据,您在关注吗?

However, the products in the database and especially the prices could change between different days. 但是,数据库中的产品(尤其是价格)可能在不同的日期之间发生变化。 Hence, I think I need to preload the data every time I open the app. 因此,我认为每次打开应用程序时都需要预加载数据。 This will take a while, especially with slow Internet. 这将需要一段时间,尤其是在互联网速度较慢的情况下。 I am wondering if there is a better solution? 我想知道是否有更好的解决方案?

Thanks, Casper 谢谢卡斯珀

Think about what data will change and what data will not... 考虑哪些数据将发生变化,哪些数据将不会发生变化...

Consider breaking up your data into groups - those that change regularly, those that change infrequently and those that never change. 考虑将数据分为几类-定期更改的数据,不经常更改的数据和永不更改的数据。

Then you'll be able to upload the data for your app based on group. 然后,您可以根据组上传应用程序的数据。

What data may be uploaded once: 一次可以上传什么数据:

  • product names? 产品名称?
  • product images? 产品图片?

What data may be uploaded daily: 每天可以上传哪些数据:

  • product prices? 产品价格?

What data may be uploaded infrequently: 哪些数据可能不经常上传:

  • product description? 产品描述?
  • product images (when these change)? 产品图片(这些更改时)?

Perhaps there will be new products uploaded also? 也许还会有新产品上传?

I recommend that you consider use of thumbnail images and full size images. 我建议您考虑使用缩略图和全尺寸图像。

As a guide, thumbnail images should be of small dimension and file size... maybe 300 x 300 pixels and less than 150kb. 作为指导,缩略图应具有较小的尺寸和文件大小...可能为300 x 300像素且小于150kb。 Thumbnail images can be saved as a Data type using Core Data (which is persisted as a blob in SQLite). 可以使用Core Data(在SQLite中将其作为Blob保留)将缩略图图像保存为Data类型。

Full sized (large file size) images can be persisted using Core Data, however there are different ways to achieve this. 可以使用Core Data保留完整尺寸(大文件大小)的图像,但是有多种方法可以实现此目的。

There is much written, both on SO and other sites, regarding storage of large file size images using Core Data. 在SO网站和其他网站上都有很多关于使用Core Data存储大文件大小图像的文章。

My preference is to save the image file to a custom app directory/ folder in your app sandbox that is not backed up my iTunes or iCloud and save a reference to that image file (as a file path) as a String attribute in your Core Data model. 我的偏好是将图像文件保存到未备份iTunes或iCloud的应用程序沙箱中的自定义应用程序目录/文件夹中,并将对该图像文件的引用(作为文件路径)保存为Core Data中的String属性。模型。 In this way you're able to persist the image files locally and only upload the changes, when necessary. 这样,您可以将图像文件持久保存在本地,并且仅在必要时上载更改。

So by using different groups of data, you can also spilt up the upload into chunks... those that are needed: 因此,通过使用不同的数据组,您还可以将上载分散到大块……所需的那些块中:

  • as soon as the app runs (eg product names); 应用运行后(例如产品名称);
  • early in the app lifecycle (eg thumbnail images); 在应用生命周期的早期(例如缩略图);
  • in the background (eg full sized images). 在背景中(例如,全尺寸图像)。

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

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