parse.com 从 currentUser 获取指针数据

parse.com getting pointer data from currentUser

我在 PFUser 中有一个指针键,我正在尝试检索它指向的对象。我已经看到很多关于查询它的例子,但不应该有任何需要,因为 parse 有 fetch 方法并且它是 PFUser 类的指针,所以我使用这个:

PFObject *menu = [[[PFUser currentUser] objectForKey:@"menuID"] fetchIfNeeded];

我知道我当前的用户在那个键中有一个指向的对象,但我一直得到一个空菜单对象

默认情况下 currentUser 没有填充任何自定义添加的数据列。您需要让用户下载该数据,然后您可以在本地使用它。

或者你的油我们云代码并保存网络请求。

Wain 说您需要获取 currentUser 是正确的。但是,如果您想使用 fetchInBackground,您必须记住我们正在使用多个线程。要保持在单个线程中,只需使用 [[PFUser currentUser] fetch],但请记住,当互联网连接不良时,这可能会导致用户挂起或阻塞。以下是如何更有效地使用它的示例。 (与菜单的 fetch 和 fetchInBackground 有同样的问题)我们也必须获取菜单,因为它是一个指针,所以 currentUser 将只获取指针而不是整个对象。

[[PFUser currentUser] fetchInBackgroundWithBlock:^(PFObject *object, NSError *error) {
    if(!error){
        PFObject *menu = object[@"menuID"];
        [menu fetch];
        // Execute any code that needs the menu here, or store it in your viewController (or other class variable) if you need to save it for later  

        // Alternately, you could use this:
        dispatch_async(dispatch_get_main_queue(), ^{
            [menu fetchInBackgroundWithBlock:^(PFObject *fetchedMenu, NSError *menuError) {
                if(!menuError){
                    // Execute any code that needs the menu here, or store it
                } else {
                    NSLog(@"%@", menuError);
                }
            }];
        });
    } else {
        NSLog(@"%@", error);
    }
}];