I'm trying to loop through an array and add items to a new array. The addObject
part exists inside a block. Something seems to be preventing any objects from being added to my new array. I'm wondering if the block is causing problems.
It goes like this:
-(void)queryForID
{
NSLog(@"QUERY FOR ID");
self.REST = [[AIREST alloc] init];
[self.REST querySchema:@"business" searchString:@"object_id" fromArray:self.json.objects];
}
AIREST.h
#import <Foundation/Foundation.h>
#import "StackMob.h"
@interface AIREST : NSObject
-(void)querySchema:(NSString *)schema searchString:(NSString *)searchString fromArray:(NSArray *)searchArray;
@property (nonatomic, strong) NSMutableArray *queryResults;
@end
AIREST.m
-(void)querySchema:(NSString *)schema searchString:(NSString *)searchString fromArray:(NSArray *)searchArray
{
self.queryResults = [[NSMutableArray alloc] init];
for (int i = 0; i < [searchArray count]; i++)
{
MyObject *myObject = searchArray[i];
SMQuery *newQuery = [[SMQuery alloc] initWithSchema:schema];
[newQuery where:searchString isEqualTo:myObject.objectID];
[[[SMClient defaultClient] dataStore] performQuery:newQuery onSuccess:^(NSArray *results) {
// results contains an array of dictionary objects that match the query
if ([results count] > 0)
{
[self.queryResults addObject:results];
NSLog(@"QUERY: %@", self.queryResults);
}
} onFailure:^(NSError *error) {
// Error
NSLog(@"QUERY ERROR: %@", error);
}];
}
[[NSNotificationCenter defaultCenter] postNotificationName:@"gotQueryResults" object:nil];
}
At the end of the loop, NSLog
prints the contents of self.queryResults
, except self.queryResults
appears empty afterwards. Does this make any sense?