ios - 从 Parse 刷新和更新 NSMutableA rray 中的对象

标签 ios iphone nsmutablearray parse-platform

我有一个用户注册的应用程序,每次他们完成一个级别时,它都会被发送到 Parse。到目前为止一切都很好。现在,当用户想要添加 friend 时,他们输入他们的电子邮件,然后将他们的名字添加到 NSMutableArray 并将他们的分数添加到单独的 NSMutableArray。现在,我需要一些方法来将它们更新为可用的最新乐谱。所以这就是我所拥有的:

// This is called when an add button is pressed

- (void)launchingAlertForEmail {

    UIAlertView *emailAlertView = [[UIAlertView alloc] initWithTitle:@"Add a friend" message:@"Type in your friends email" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Add friend", nil];

    emailAlertView.alertViewStyle = UIAlertViewStylePlainTextInput;

    [emailAlertView show];

}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {

    if (buttonIndex == 1) {

        friendEmail = [[alertView textFieldAtIndex:0] text];

        [self findUserMatchingEmail];

    }

}

- (void)findUserMatchingEmail {

    PFQuery *query = [PFQuery queryWithClassName:@"User"];

    [query whereKey:@"username" equalTo:friendEmail];

    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {

        if (!error) {

            // The find succeeded.

            NSLog(@"Successfully retrieved %d scores.", objects.count);

            // Do something with the found objects

            for (PFObject *object in objects) {

                NSLog(@"%@", object.objectId);

            }

            if (objects.count == 0) {

                NSLog(@"Tis true");

            } else {

                [self insertNewObject:self];

            }

        } else {

            // Log details of the failure

            NSLog(@"Error: %@ %@", error, [error userInfo]);

        }

    }];

}

- (void)insertNewObject:(id)sender {

    if ([_objects containsObject:friendEmail]) {

        NSLog(@"yea");

    }    

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    if (!_objects) {

        _objects = [defaults mutableArrayValueForKey:@"_objects"];

    }

    [_objects insertObject:friendEmail atIndex:0];

    if (!objects2) {

        objects2 = [defaults mutableArrayValueForKey:@"objects2"];

    }

    PFQuery *query = [PFQuery queryWithClassName:@"User"];

    [query whereKey:@"username" equalTo:friendEmail];

    NSArray *usersPosts = [query findObjects];

    for (NSDictionary *friendData in usersPosts) {

        NSString *intervalString = [friendData objectForKey:@"interval"];

        float interval = [intervalString floatValue];

        NSString *newInterval = [NSString stringWithFormat:@"Interval : %.0f minutes", interval / 60];

        [objects2 insertObject:newInterval atIndex:0];

    }

    if ([_objects containsObject:friendEmail]) {

        NSLog(@"yea");

    }

    [defaults setObject:_objects forKey:@"_objects"];

    [defaults setObject:objects2 forKey:@"objects2"];

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];

    [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

} 

这会在应用关闭并重新打开时成功加载数据。现在,我需要它用新数据刷新并选择在每次出现 View 时都这样做。

那么我可以在 ViewDidAppear 中做什么来更新电子邮件的每个值。所以我在想,每封电子邮件都会更改另一个 NSMutableArray 中的值,因为索引是相等的。问题是如何更改相反数组中每封电子邮件的值?我会使用 for 循环吗?这看起来行得通吗?

最佳答案

在查询中,使用 whereKey:matchesQuery:查找所有与 friend 关联的分数对象(子查询是查找 friend )。您还想使用 includeKey:以便 friend 有空。

关于ios - 从 Parse 刷新和更新 NSMutableA rray 中的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20925776/

相关文章:

ios - 当应用程序直接在设备中运行并创建 ipa 文件时,为什么设备 token 生成不同?

ios - 无法快速将 NSMutableArray 的值存储在 NSUserdefault 中

objective-c - NSMutableArray addObject 不起作用

ios - Ionic 5 Capacitor - 如何在IOS上动态更改状态栏背景颜色

ios - Xcode 未找到匹配的私钥

ios - 检查 indexPath 处的单元格在屏幕 UICollectionView 上是否可见

ios - 使用 swift 在 ios 上集群谷歌地图标记

iphone - 苹果商店更新?

iphone - 屏幕分辨率和帧尺寸?

ios - 当我从 NSMutableArray 获取对象时,为什么我的对象为零?