ios - 如何在Parse iOS中避免数据重复?

标签 ios facebook parse-platform

我是使用Parse在iOS中成为新手,我制作了一个包含Facebook登录名的应用程序,如果用户已经登录,则要创建该应用程序,然后将其数据放入我的Parse Data表中,如下所示:

PFObject *userInfo = [PFObject objectWithClassName:@"User"];
        userInfo[@"email"]=self.email;
        userInfo[@"name"]=self.username;
        [userInfo saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error)
         {
             if (succeeded)
             {
                 NSLog(@"Do whatever you want to Do");
             }
             else{
                 NSString *errorString = [[error userInfo] objectForKey:@"error"];
                 NSLog(@"Error: %@", errorString);
             }

         }];

但是它包含每个相同用户的重复数据,所以我为此编写了代码
PFQuery *query = [PFQuery queryWithClassName:@"User"];
[query whereKey:@"name" equalTo:self.username];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error)
{
    NSLog(@"%@",objects);
    if ([objects containsObject:self.username])
    {
        NSLog(@"Successfully retrieved: %@", objects);
    }
    else
    {
        PFObject *userInfo = [PFObject objectWithClassName:@"User"];
        userInfo[@"email"]=self.email;
        userInfo[@"name"]=self.username;
        [userInfo saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error)
         {
             if (succeeded)
             {
                 NSLog(@"Do whatever you want to Do");
             }
             else{
                 NSString *errorString = [[error userInfo] objectForKey:@"error"];
                 NSLog(@"Error: %@", errorString);
             }

         }];
    }    }];

但它的工作原理与上面相同,在这里我想让如果用户已经登录,那么它的数据就不会每次都插入表中。
请给我解决方案。

最佳答案

我不知道您要做什么,但似乎您正在尝试创建自己的用户对象。我会改用内置的。链接到文档。加上下面将其与facebook一起使用的简单示例。它不包含所有内容。
https://www.parse.com/docs/ios/guide#users-logging-in

NSArray *permissions = @[kLoginPermissionPublicProfile, kLoginPermissionEmail, kLoginPermissionFitness, kLoginPermissionBirthday];

if ([PFUser currentUser] == nil) {
    if(facebookSignin){
        [PFFacebookUtils logInWithPermissions:permissions block:^(PFUser *user, NSError *error) {
            //Pull any facebook data and save it
        }];
    }else if(signUp){
        PFUser *user = [PFUser user];
        user.username = email;
        user.password = password;
        user.email = email;

        [user signUpInBackgroundWithBlock:^(BOOL success, NSError* error){
            //add data and save it
        }];
    }else{
        [PFUser logInWithUsernameInBackground:email password:password block:^(PFUser* user, NSError* error) {
            if (!error) {
                if (user) {
                    //add data and save it
                }
            }
        }];
    }
}else{
    PFUser *user = [PFUser currentUser];
    user[@"phone"] = @"415-392-0202";
    [userInfo saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error){
        //ToDo
    }];
}

关于ios - 如何在Parse iOS中避免数据重复?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28450089/

相关文章:

javascript - 如何使用 Parse.com/Express JS 检索对象信息并将其添加到数组中

android - 错误包 `com.google.android.gms...` 不存在

ios - 仅实例化一个时, View Controller 中会显示两个 UISearchBar

ios - 如何在iOS中制作元素网格?

swift - Facebook Swift SDK 登录不会打开 native 对话框

css - 检查 facebook 图像时奇怪的 CSS 背景图像 url

javascript - Facebook Javascript fb.ui 事件在 friend 墙上张贴照片?

php - 如何在 php 中检索 Parse.com 用户详细信息

ios - 对于 Uiimageview 如何将 CGgradient 背景色设置为 ios swift 4 中的背景色?

iphone - iPhone 上缺少 touchEnd 事件