Objective c 在后台解析、计数对象

Objective c parse, count objects in background

我尝试获取在后台找到的查询数量。所以我使用 countobjects 方法来获取数字,但系统对此有警告 "A long-running operation is being executed on the main thread."

PFQuery *query2=[PFQuery queryWithClassName:@"Comments"];
[query2 whereKey:@"Name" equalTo:globalName];
NSInteger CommentPoint=query2.countObjects;

所以我改用这种方法,但我无法在我的控制台中打印计数。

 [query2 countObjectsInBackgroundWithBlock:^(int count, NSError *error) 
{
   if (!error) {
        // The count request succeeded. Log the count
       NSLog(@"Sean has played %d games", count);
   } else {
       // The request failed
   }
}];

我的另一个问题是如何将 "count" 分配给全局 NSinteger?

我试过了

PFQuery *query=[PFQuery queryWithClassName:@"AllPost"];
[query whereKey:@"PostUser" equalTo:globalName];
NSLog(@"globalname %@",globalName);
[query countObjectsInBackgroundWithBlock:^(int count, NSError *error)
{
    if (!error) {
        self.objectCount = [NSNumber numberWithInt:count];
        // The count request succeeded. Log the count
        NSLog(@"Sean has played %d games", count);
    } else {
        NSLog(@"error");
    }
}];

NSLog(@"objectCount %@",self.objectCount);

这是控制台打印出来的内容

2015-07-02 19:17:16.721 ParseNews[12598:608067] globalname Sheng
2015-07-02 19:17:16.721 ParseNews[12598:608067] objectCount (null)

'how to save' 的一个解决方案可能是创建一个全局 NSNumber 属性:

@property (strong, nonatomic) NSNumber *objectCount;

然后,块内:

self.objectCount = [NSNumber numberWithInt:count];