简体   繁体   English

保存和加载UITableView数组 - iOS Swift Parse

[英]Saving and loading UITableView array - iOS Swift Parse

I have the following; 我有以下几点; which fetches the data from the Parse backend and loads the class instances into a NSArray to plug into a UITableView . 它从Parse后端获取数据并将类实例加载到NSArray以插入UITableView

var ObjectIDClass = PFQuery(className: "TestObject")

ObjectIDClass.findObjectsInBackgroundWithBlock({
  (ObjectsArray : [AnyObject]?, error: NSError?) -> Void in

  var ObjectIDs = ObjectsArray as! [PFObject]
  for i in 0...ObjectIDs.count-1 {
    self.iDArray.append(ObjectIDs[i].valueForKey("objectId") as! String)
    self.NameArray.append(ObjectIDs[i].valueForKey("PDFName") as! String)
    self.tableView.reloadData()
  }
})

This only works with network connection and as soon as the application is closed and then reopened without network, the table is blank. 这仅适用于网络连接,一旦应用程序关闭,然后在没有网络的情况下重新打开,表格就是空白。 What I am aiming for is to store the data in the Local Datastore and if there is no connectivity, load from the Local Datastore . 我的目标是将数据存储在Local Datastore ,如果没有连接,则从Local Datastore加载。 I can't figure it out using pin , but sure that is how it is done. 我无法用pin ,但确定它是如何完成的。 Any help would be hugely appreciated! 任何帮助将非常感谢!

Instead of focusing strictly on Local Datastore , which isn't a bad thing. 而不是严格关注Local Datastore ,这不是一件坏事。 I just feel you are using that solely because you want to have access to that data, say when the user is in airplane mode, or when there are just low network conditions. 我只是觉得你正在使用它只是因为你想要访问这些数据,例如当用户处于飞行模式时,或者当网络条件很低时。 If that is the case, then you can take advantage of Parse's built in cache system. 如果是这种情况,那么您可以利用Parse的内置缓存系统。 The PFQuery cache by default is set to ignore cache. 默认情况下, PFQuery缓存设置为忽略缓存。 This means, none of the information incoming from the server is stored in a temp folder on the users hard disk (exactly what your looking for). 这意味着,从服务器传入的信息都不会存储在用户硬盘上的临时文件夹中(正是您要查找的内容)。 So you have to explicitly tell the objects to be cached on device using one of their many caching policies 因此,您必须使用其众多缓存策略之一明确告知对象在设备上缓存

For you, this will be good: 对你来说,这将是好的:

query.cachePolicy = .CacheElseNetwork
// Results were successfully found, looking first on the
// disk and then on network.
query.findObjectsInBackgroundWithBlock {

You can review other ways to cache with these options : https://www.parse.com/docs/ios/guide#queries-caching-queries 您可以使用以下选项查看其他缓存方式: https//www.parse.com/docs/ios/guide#queries-caching-queries

Small side note, please don't uppercase your variables. 小方注意,请不要大写你的变量。 It doesn't follow proper naming conventions. 它没有遵循正确的命名约定。 ObjectIdDClass should resemble, somewhat, what your going to do with it's class, so it should be called query or something. ObjectIdDClass某种程度上应该类似于你要对它的类做什么,所以它应该被称为query或其他东西。 And ObjectsArray should be objects because you already know it's an array and you don't capitalize anything that's not a class, usually. ObjectsArray应该是objects因为你已经知道它是一个数组,并且通常不会将任何非类的东西大写。 It makes for easier reading 它使阅读更容易

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

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