在Parse iOS中更新对象,[错误]:找不到对象

时间:2016-08-06 00:57:52

标签: ios swift parse-platform

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.

    // Set up the Parse SDK
    let configuration = ParseClientConfiguration {
        $0.applicationId = "WhatsTheHW"
        $0.server = "https://whatsthehw-parse-alan.herokuapp.com/parse"
    }
    Parse.initializeWithConfiguration(configuration)

    let query = PFQuery(className: "Course")

    query.findObjectsInBackgroundWithBlock {(result: [PFObject]?, error: NSError?) -> Void in
        for object in result! {
            // existing objectIds: 1Ja2Hx77zA, 34AF1vKO6f, 5FWlsswxw0
            if object.objectId == "34AF1vKO6f" {
                object["studentRelation"] = ["hi", "ih"]
                object.saveInBackgroundWithBlock{(success, error) in
                    if success == true {
                        print("\(object) saved to parse")
                    } else {
                        print("save failed: \(error)")
                    }
                }
            }
        }
    }

    return true
}

这是我可以将此任务减少到的最小值(此代码位于AppDelegate)。

当我在解析仪表板中尝试使用REST api和api控制台时,一切正常,但它不能与iOS sdk一起使用。

我从print语句得到的错误是

Error Domain=Parse Code=101 "Object not found." UserInfo={code=101, temporary=0, error=Object not found., NSLocalizedDescription=Object not found.}

如果我只是添加一个像这样的新对象,它就可以了:

let object = PFObject(className: "Course")
object["name"] = "German"
object["studentRelation"] = ["a", "b"]

object.saveInBackgroundWithBlock{(success, error) in
    if success == true {
        print("save completed")
        print("\(object) saved to parse")
    } else {
        print("save failed: \(error)")
    }
}

我真的输了,我不知道为什么会这样。

提前致谢。

2 个答案:

答案 0 :(得分:2)

此问题可能与您尝试保存的对象的访问权限ACL)有关。当没有[Error]: Object not found对象访问权限的用户试图保存它时,会打印write,Parse SDK的错误消息在这里真的会产生误导!

确保尝试保存对象的解析用户对解析数据库中该对象上的实际 write 进行了正确写入。

一个简单的解决方法是将应用内的默认ACL设置为public read + write

let acl = PFACL()
acl.publicReadAccess = true
acl.publicWriteAccess = true
PFACL.setDefaultACL(acl, withAccessForCurrentUser: true)

请注意这种方法,通常您希望根据用户的实际角色设置访问权限。因此,更好的选择是仅在您创建ACL时设置PFObject并仅向write访问您知道应该能够更改的用户对象

答案 1 :(得分:0)

你能先找到你的对象,保存到另一个对象中并在循环外运行saveInBackground。

您的代码应如下所示:

    var objectToSave : PFObject?

    let query = PFQuery(className: "Course")
    query.findObjectsInBackgroundWithBlock {(result: [PFObject]?, error: NSError?) -> Void in
        for object in result! {
            if object.objectId == "jMIxdSXNRH" {
                objectToSave = object
            }
        }


        if objectToSave != nil {
            objectToSave!["studentRelation"] = ["hi", "ih"]
            objectToSave!.saveInBackgroundWithBlock{(success, error) in
                if success == true {
                    print("\(objectToSave) saved to parse")
                } else {
                    print("save failed: \(error)")
                }
            }
        }

    }

我正在使用我的objectId而不是你的,请将它们改为你的:)