i using parse setup user pairs , save matchedrelation in userrelations class. however, found weird issue when append pfobject pfobject array, elements in array replaced, while totally fine when using anyobject array. please me find out wrong.
query.findobjectsinbackgroundwithblock { (objects: [anyobject]!, error: nserror!) -> void in if error == nil { var pairrelation = pfobject (classname: "userrelations") var pairrelations = [pfobject]() var testarray = [anyobject]() object in objects { pairrelation.setobject(object.username, forkey: "touser") pairrelation.setobject(pfuser.currentuser()!, forkey: "fromuser") pairrelation.setobject(true, forkey: "ismatched") pairrelations.append(pairrelation) testarray.append(object.username) println("after append results of array is: \(pairrelations)") println("\nafter append results of test array is: \(testarray)") } } } the output first 3 matches shown here:
after append results of array is: [<userrelations: 0x7fe22b0713c0, objectid: new, localid: (null)> { fromuser = "<pfuser: 0x7fe22b127670, objectid: 3wxg5fuese>"; ismatched = 1; touser = "asdfsdf@df.sdfss";}, <userrelations: 0x7fe22b0713c0, objectid: new, localid: (null)> { fromuser = "<pfuser: 0x7fe22b127670, objectid: 3wxg5fuese>"; ismatched = 1; touser = "asdfsdf@df.sdfss";}, <userrelations: 0x7fe22b0713c0, objectid: new, localid: (null)> { fromuser = "<pfuser: 0x7fe22b127670, objectid: 3wxg5fuese>"; ismatched = 1; touser = "asdfsdf@df.sdfss";}] after append results of test array is: [andy@gd.com, dfasdf@fsadf.dfs, asdfsdf@df.sdfss] so pfobject array got same elements after appending, while anther array got 3 different users. comment/help!
you creating 1 instance of pfobject named pairrelation. inside loop updating same object in memory.
just move line inside loop create new pfobject each time:
query.findobjectsinbackgroundwithblock { (objects: [anyobject]!, error: nserror!) -> void in if error == nil { var pairrelations = [pfobject]() var testarray = [anyobject]() object in objects { var pairrelation = pfobject (classname: "userrelations") //create new pfobject pairrelation.setobject(object.username, forkey: "touser") pairrelation.setobject(pfuser.currentuser()!, forkey: "fromuser") pairrelation.setobject(true, forkey: "ismatched") pairrelations.append(pairrelation) testarray.append(object.username) println("after append results of array is: \(pairrelations)") println("\nafter append results of test array is: \(testarray)") } } }
Comments
Post a Comment