简体   繁体   English

如何保存相关的核心数据对象以快速分析

[英]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. 有所有者的实体,它们拥有实体词表以及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. 在一个屏幕中,我希望能够保存一个wordList及其相关单词和所有者以按一下按钮即可进行解析。 Then in another screen I want to be able to download a WordList and its related words, then save it to core data. 然后在另一个屏幕中,我希望能够下载WordList及其相关词,然后将其保存到核心数据中。 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. 对于大多数项目,这可以正常上传,但是与wordList相关的单词和所有者没有保存。 在此处输入图片说明

Is it possible to use the relationship properties like this with Parse? 是否可以在Parse中使用这样的关系属性? How would I then get a shared wordList and all its properties back from Parse into CoreData? 然后,我将如何将共享的wordList及其所有属性从Parse返回到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. 此代码"\\(wordList?.words)"正在获得关系内容的可读描述。 That is a log description of the NSSet of managed objects. 这是受管理对象的NSSet的日志描述。 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. 您真正想要做的是获取关系,然后询问每个项目的name You can do that with KVC. 您可以使用KVC做到这一点。 When you have that it would be an NSSet of strings that you can use to store directly. 如果有,它将是一个NSSet字符串,可用于直接存储。

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. 还可以使用REST接口与RestKit之类的库进行解析,以将解析数据存储中的内容直接映射到核心数据中。

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

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