objective c - Delete PFUser with Cloud Code Parse.com iOS -


i succeed add friend cloud code , parse.com.

now delete friend relation cloud code in didselectrowatindexpath

my error "attempt insert nil object objects[0]'"

but don't know parameters need configure, found cloud code main.js :

parse.cloud.define("removefriend", function(request, response)  {     // here's how client's user making request     var user = request.user;      // consider checking user && request.params.friend valid     // if not, return response.error("missing user or friend id")      getuser(request.params.friend).then(function(friend) {         // code prematurely called response.success() here, thereby canceling further steps         friend.relation("friendsrelation").remove(user);         // return promise returned save() can chain promises         return friend.save();     }).then(function(result) {         // save finished, can claim victory         response.success(result);     }, function (error) {         response.error(result);     }); });  // edit - op once referred getuser function assume this: // return promise user userid function getuser(userid) {     var userquery = new parse.query(parse.user);     return userquery.get(userid); } 

here code editfriends.m :

    - (void)viewdidload     {         [super viewdidload];         pfquery *query = [pfuser query];         [query orderbyascending:@"name"];         [query findobjectsinbackgroundwithblock:^(nsarray *objects, nserror *error) {             if (error) {                 nslog(@"error: %@ %@", error, [error userinfo]);             }             else {                 self.allusers = objects;                 [self.tableview performselectoronmainthread:@selector(reloaddata) withobject:nil waituntildone:no];             }         }];          self.currentuser = [pfuser currentuser];         [self loadfriends];     }  -(void) loadfriends{     self.friendsrelation = [[pfuser currentuser] objectforkey:@"friends"];     pfquery *query = [self.friendsrelation query];     [query orderbyascending:@"username"];     [query findobjectsinbackgroundwithblock:^(nsarray *objects, nserror *error)      {          if (error) {              nslog(@"error %@ %@", error, [error userinfo]);          }          else {              self.friends = objects;               [self.tableview reloaddata];          }      }]; }       - (bool)isfriend:(pfuser *)user {         for(pfuser *friend in self.friends) {             if ([friend.objectid isequaltostring:user.objectid]) {                 return yes;             }         }          return no;     } 

cellforrowatindexpath :

- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath {     static nsstring *cellidentifier = @"cell";     uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:cellidentifier forindexpath:indexpath];      pfuser *user = [self.allusers objectatindex:indexpath.row];      nsstring *name = [[self.allusers objectatindex:indexpath.row] valueforkey:@"username"];     cell.textlabel.text = name;      if ([self isfriend:user]) {         cell.accessorytype = uitableviewcellaccessorycheckmark;      } else {          cell.accessorytype = uitableviewcellaccessorynone;     }      return cell; } 

didselectrowatindexpath :

pfuser *selected = [self.allusers objectatindex:indexpath.row];     if ([self isfriend:selected]) {         nslog(@"déjà amis"); //        pfobject *friendrequest = [self.friendrequests objectatindex:indexpath.row];         [pfcloud callfunctioninbackground:@"removefriend" withparameters:@{@"friendrequest" : selected.objectid} block:^(id object, nserror *error) {              if (!error) {                 //add fromuser currentusers friends                  //save current user                 [self.currentuser saveinbackgroundwithblock:^(bool succeeded, nserror *error) {                      if (succeeded) {                        } else {                      }                  }];              }             else {              }           }];      } else{     pfuser *selecteduser = [self.allusers objectatindex:indexpath.row];     //request them     pfobject *friendrequest = [pfobject objectwithclassname:@"friendrequest"];     friendrequest[@"from"] = self.currentuser;     friendrequest[@"fromusername"] = [[pfuser currentuser] objectforkey:@"username"];     //selected user user @ cell selected     friendrequest[@"to"] = selecteduser;     // set initial status pending     friendrequest[@"status"] = @"pending";     [friendrequest saveinbackgroundwithblock:^(bool succeeded, nserror *error) {          if (succeeded) {               uialertview *alert = [[uialertview alloc] initwithtitle:@"yay" message:@"friend request sent" delegate:nil cancelbuttontitle:@"ok" otherbuttontitles:nil, nil];             [alert show];            } else {              // error occurred         }     }]; } 

enter image description here

the ios code looks okay, , needs sure sends correct user objectid.

the cloud code close, must improved little:

parse.cloud.define("removefriend", function(request, response)  {     // here's how client's user making request     var user = request.user;      // consider checking user && request.params.friend valid     // if not, return response.error("missing user or friend id")      getuser(request.params.friendrequest).then(function(friend) {         // code prematurely called response.success() here, thereby canceling further steps         console.log("relation is:" + json.stringify(friend.relation("friends")));         friend.relation("friends").remove(user);         // return promise returned save() can chain promises         return friend.save();     }).then(function(friend) {         // friendship goes both ways, remove friend user's friends         user.relation("friends").remove(friend);         return user.save();     }).then(function(result) {         // save finished, can claim victory         console.log("relation is:" + json.stringify(result.relation("friends")));         response.success(result);     }, function (error) {         response.error(error);     }); });  // edit - op once referred getuser function assume this: // return promise user userid function getuser(userid) {     var userquery = new parse.query(parse.user);     return userquery.get(userid); } 

edit - call:

pfuser *selected = [self.allusers objectatindex:indexpath.row]; if ([self isfriend:selected]) {     nslog(@"déjà amis");     [pfcloud callfunctioninbackground:@"removefriend" withparameters:@{@"friendrequest" : selected.objectid} block:^(id object, nserror *error) {     // etc. 

Comments

Popular posts from this blog

IF statement in MySQL trigger -

c++ - What does MSC in "// appease MSC" comments mean? -

javascript - Blogger related post gadget image Resize s72-c [ Need Expert Help ] -