我是使用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/