简体   繁体   中英

Parse Local Datastore returns only one object

I have the following code:

var episodesToSendToParse = [PFObject]()
let currentPodcast = PFObject(className: "Podcast")

currentPodcast["name"] = name
currentP["user"] = PFUser.currentUser()

for episode in episodes {
  let episodes2 = PFObject(className: self.episodesClass)
  episodesToParse["title"] = episode.title
  episodesToParse["parent"] = currentPodcast
  episodesToSendToParse.append(episodes2)
}

PFObject.pinAllInBackground(episodesToSendToParse).continueWithBlock { (task: BFTask!) -> AnyObject! in
        if let error = task.error {
            print(error)
            return task;
        }
        print("finished pinning")
        return task;
    }

PFObject.saveAllInBackground(episodesToSendToParse).continueWithBlock { (task: BFTask!) -> AnyObject! in
        if let error = task.error {
            print(error)
            return task;
        }
        NSNotificationCenter.defaultCenter().postNotificationName("podcastSaved", object: self)
        print("finished saving")
        return task;
    }
}

It saves fine to Parse's servers. The podcast episodes also save locally but the podcast itself overwrites any other podcast I had previously saved, causing my tableview to always load 1 entry only. I thought this may be because of the following (taken from parse.com):

There are a couple of side effects of enabling the local datastore that you should be aware of. When enabled, there will only be one instance of any given PFObject. For example, imagine you have an instance of the "GameScore" class with an objectId of "xWMyZ4YEGZ", and then you issue a PFQuery for all instances of "GameScore" with that objectId. The result will be the same instance of the object you already have in memory.

However I have no idea how to remedy this.

I am running Parse 1.10.0

You have typo in code.

Replace this:

for episode in episodes {
  let episodes = PFObject(className: self.episodesClass)
  episodesToParse["title"] = episode.title
  episodesToParse["parent"] = currentPodcast
  episodesToSendToParse.append(episodesToParse)
}

With this:

for episode in episodes {
  let episodes = PFObject(className: self.episodesClass)
  episodes["title"] = episode.title
  episodes["parent"] = currentPodcast
  episodesToSendToParse.append(episodes)
}

This is a Parse bug. See: #535

Using pinAllInBackground(objects: [PFObject]?, withName: String) is a workaround.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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