问题
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:
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];
来源:https://stackoverflow.com/questions/31149163/objective-c-parse-count-objects-in-background