简体   繁体   中英

how to save related core data objects to parse with swift

My app is a simple word list sharing app. There are entities of Owners who own entities Wordlists with entities of related words in CoreData. In one screen I want to be able to save a wordList and its related words and owner to Parse on the push of a button. Then in another screen I want to be able to download a WordList and its related words, then save it to core data. present the name of the list in a table. The code I have is:

// To save the wordList to Parse:

@IBAction func shareWordList(sender: AnyObject) {

    let parseWordList = PFObject(className: "WordList")
    parseWordList.setObject("\(wordList?.listName)", forKey: "ListName")
    parseWordList.setObject("\(wordList?.owner)", forKey: "Owner")
    parseWordList.setObject("\(wordList?.words)", forKey: "Words")
    parseWordList.setObject("\(wordList?.isSharedDate)", forKey:     "IsSharedDate")
    parseWordList.setObject("\(wordList?.isShared)", forKey: "IsShared")
    parseWordList.setObject("\(wordList?.createdDate)", forKey: "CreatedDate")
    parseWordList.setObject("\(wordList?.isAppList)", forKey: "IsAppList")

    parseWordList.saveInBackgroundWithBlock { (succeeded, error) -> Void in
        if succeeded {
            print("object uploaded")
        } else {
            print("Error: \(error) \(error?.userInfo)")
        }
    }

This uploads ok for most items, but the words and owner related to the wordList are not saving. 在此处输入图片说明

Is it possible to use the relationship properties like this with Parse? How would I then get a shared wordList and all its properties back from Parse into CoreData?

Thanks in advance to anyone for some help with this....

This code "\\(wordList?.words)" is getting the human readable description of the relationship contents. That is a log description of the NSSet of managed objects. That's why you get basically gibberish in the parse data store.

What you really want to do is to get the relationship and then ask for the name of each item. You can do that with KVC. When you have that it would be an NSSet of strings that you can use to store directly.

Arguably it would be better to have multiple different classes in the parse data store which match the entities in your core data model. If you do that then you can process the relationship items to create new objects in the parse data store and then add them (once saved) to parse relationships.

It's also possible to use the REST interface to parse with a library like RestKit to map from your parse data store contents directly into core data.

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