简体   繁体   English

Parse.com保存对象一次

[英]Parse.com save object once

I am saving a PFObject called item, into a class called Objects . 我将一个名为item的PFObject保存到一个名为Objects的类中。 So the class is Objects , and in that class I have a column called item (which are string objects). 因此,该类是Objects ,在该类中,我有一列称为item(它们是字符串对象)。 Every time a row in my tableView is tapped, the app saves the text of that row to parse. 每次点击tableView的一行时,应用程序都会保存该行的文本以进行分析。

And if you tap this row 3 times for example, it will save the text 3 different times. 例如,如果您点击此行3次,它将保存文本3次不同的时间。 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. 我可以使用if语句检查该字符串是否已存在于解析中,如果存在则不保存。

Update: 更新:

If I have a class called MenuItem. 如果我有一个叫做MenuItem的类。 In MenuItem, I have a string column called item. 在MenuItem中,我有一个名为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? 我只是缺少if语句来检查它是否已经存在?

Thanks 谢谢

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. 如果您必须保存一个NSString变量,我将在User类中创建一个Item列并将其保存在其中。 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. 保存字符串之前,可以查询Objects类并遵循一些不同的逻辑。 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. 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. 不确定是哪个操作上载了内容,如果使用按钮仅为不同的状态设置了不同的颜色,或者如果使用单元格进行了操作,则只需编辑所选单元格的颜色即可。

一种实现方法是,编写一个解析查询(PFQuery到item class)以查明用户是否已经被用户点击,如果没有,则用户会将其保存为新对象,否则无需保存再来一次。

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. 如果您必须保存一个NSString变量,我将在User类中创建一个Item列并将其保存在其中。 In this way if a user taps the same cell multiple times he will just override the same string again and again. 这样,如果用户多次敲击同一单元格,他将一次又一次覆盖同一字符串。

[saveInBackgroud];

使用后台保存

[parseObject saveInBackgroud];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM