user3692917
user3692917

Reputation:

Parse.com save object once

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

Upvotes: 0

Views: 720

Answers (4)

user4048334
user4048334

Reputation:

Use save in background

[parseObject saveInBackgroud];

Upvotes: 0

iqueqiorio
iqueqiorio

Reputation: 1187

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];

Upvotes: 0

rihekopo
rihekopo

Reputation: 3350

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.

Upvotes: 1

savana
savana

Reputation: 221

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.

Upvotes: 0

Related Questions