-2

I write a code that get recipes in form of JSON from a server and present it into a UItable.

NSURL *url = [NSURL URLWithString:@"http://domain.com/recipes"];
    [config setHTTPAdditionalHeaders:@{@"Authorization":@" Token token=\"3f71235466468b7f7\""}];

    NSURLSession *session = [NSURLSession sessionWithConfiguration:config delegate:nil delegateQueue:[NSOperationQueue mainQueue]];

    [[session dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        recipes= [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
       [self.tableView reloadData];

    }
      ] resume];

This server is written in Ruby. Now I want to write a request to delete a recipe. Here it is the instruction of my server . It said that I should use following curl to delete a recipes:

Delete recipes DELETE: /recipes/:id In Curl it would be something like that:

curl -H 'Authorization: Token token="0b774d575632b"' -X DELETE http://domain.com/recipes/22 

I would like to implement the delete method. Infact, I knew that I need to implement following method.

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
//how to delete it from server??????  
// Remove the row from data model
[recipes removeObjectAtIndex:indexPath.row];

// Request table view to reload
[tableView reloadData];
} 

Does anybody know how can I implement delete method?

MKH
  • 153
  • 1
  • 3
  • 15
  • 1
    what is "backend" ? how is your backend implemented ? how can someone delete something using your backend ? – ogres Jul 31 '15 at 11:17
  • by backend word, I mean server. I did not implement it. it is third party API. the server let people to register and receive recipes from it. the server also let the user to customise recipes list by adding, editing and deleting recipes. @ogres – MKH Jul 31 '15 at 14:17

2 Answers2

1

Hi i guess you are logging to delete a row from the tableview and the backed. i guess this link will help you. delete a row

if the user has deleted any row that he doesn't want to see then it can be deleted by tableview by this method

 [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];

and after that you have to remove it form your backed like if your backed is local database then you have to fire a delete query and if you are using webservice then hit the webservice.

Community
  • 1
  • 1
Parv Bhasker
  • 1,773
  • 1
  • 20
  • 39
1

I think, you need this:

NSString *query=[NSString stringWithFormat:@"delete from Inbox_Table where msg_ID='%d'",obj.msg_ID]; //whatever your delete query
[DataBaseClass deleteRecord:[query UTF8String]]; //call method to delete from database

[testArr removeObjectAtIndex:indexPath.row]; //remove from array
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; //remove row from tableview
Bhanupriya
  • 1,202
  • 1
  • 9
  • 20
  • Thanks for your response. I use the the second part of your answer. but I could not use the first part since my backend is on the server. – MKH Jul 31 '15 at 14:27
  • I updated the question – MKH Jul 31 '15 at 15:04