Objective c parse, count objects in background

2019-09-06 17:49发布

I try to get the number of queries I found in background. So I use countobjects method to get the number, but system has warning about it "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;

so i change to this method, but i can't get numbers of count print in my console.

 [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
   }
}];

My another question is how can I assign "count" to an Global NSinteger?

I tried this

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);

this is what console print out

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

1条回答
太酷不给撩
2楼-- · 2019-09-06 18:38

One solution for the 'how to save' could be to create a global NSNumber property:

@property (strong, nonatomic) NSNumber *objectCount;

then, inside the block:

self.objectCount = [NSNumber numberWithInt:count];
查看更多
登录 后发表回答