0

I am saving a PFObject called item, into a class called Objects. So the class is Objects, and in that class I have a column called item (which are string objects). Every time a row in my tableView is tapped, the app saves the text of that row to parse.

And if you tap this row 3 times for example, it will save the text 3 different times. Is there a way to only save it once.

Can I use an if statement to check if that string already exists in parse and if so then not save it.

Update:

If I have a class called MenuItem. In MenuItem, I have a string column called item.

Then in my app I have

menuItem[@"item"] = @"item1";
[menuItem saveInBackground];

This code runs every time the app is opened lets say.

How would I check this?

I'm looking at the documentation and I think it is something like this?

   PFQuery *query = [PFQuery queryWithClassName:@"MenuItem"];
[query whereKey:@"item" hasPrefix:@""];
[query findObjectsInBackgroundWithBlock:^(NSArray *items, NSError *error) {


    if (!error) {
        // The find succeeded.
        NSLog(@"Successfully retrieved %lu items.", (unsigned long)items.count);
        // Do something with the found objects
        for (PFObject *item in items) {
            NSLog(@"%@", item.objectId);
        }
    } else {
        // Log details of the failure
        NSLog(@"Error: %@ %@", error, [error userInfo]);
    }


}];

I am just missing the if statement to check it the item is already there?

Thanks

4 Answers4

1

It's a little bit hard to give an exact answer without code, but if i would be in your shoes, i would do this:

a. Version

If you have to save one NSString variable, i would create an Item column inside the User class and save it there. In this way if a user taps the same cell multiple times he will just override the same string again and again.

b. Version

Before you save the string you can query the Objects class and follow some different logics. My opinion is that the easiest way is to loop trough every object that was uploaded by the current user and if the user already has an object with the same string as the current cell has, just don't upload the photo and the user can tap the same cell as many times as he want.

c. Version

Display somehow that the cell was tapped, so the user will know when a cell was tapped. It's not sure that which action uploads the content, if you do it with a button just set different colors for the different states, or if you do it with a cell, just edit the color of the selected cells.

rihekopo
  • 3,241
  • 4
  • 34
  • 63
  • @user3692917 No. It's totally possible without CloudCode, everything what you need is in the Parse iOS guide. – rihekopo Jul 06 '14 at 18:31
  • I am looking there in my updated answer, did I start correctly? –  Jul 06 '14 at 18:40
  • @user3692917 i would use equalTo: instead of hasPrefix:, and you also have to filter to the current users. This way you get objects from every user. – rihekopo Jul 06 '14 at 18:48
0

One way of doing it is, writing a parse query(PFQuery to item class ) to find out if the user has already tapped by the user or not, if not then the user will save it as a new object else there's no need of saving it again.

savana
  • 221
  • 2
  • 5
  • Could you give an general example of how to do this. Would you need cloud code or just the PFQuery. I will update my question with some details –  Jul 06 '14 at 18:32
0

This should do it

If you have to save one NSString variable, i would create an Item column inside the User class and save it there. In this way if a user taps the same cell multiple times he will just override the same string again and again.

[saveInBackgroud];
iqueqiorio
  • 1,149
  • 2
  • 35
  • 78
0

Use save in background

[parseObject saveInBackgroud];
mmc
  • 17,354
  • 2
  • 34
  • 52