我有一个名为“ malls”的NSArray,其中包含大量我上载到Parse.com的NSDictionaries(每个特定的购物中心)。 我希望我的用户能够访问此信息以创建地图注释。
我尝试用两种不同的方法来做到这一点:
- 我尝试上传整个数组作为单个对象的属性:
这是上传:
在dataBank.h文件中:
@property (strong, nonatomic) NSMutableArray* malls;
在.m文件中
PFObject *obj = [PFObject objectWithClassName:@"malls"];
obj[@"mallsData"] = self.malls;
[obj saveInBackground];
我尝试从解析中获取数据:
-(NSMutableArray *)createAnnotationsFromParse
{
__block NSMutableArray* data = [[NSMutableArray alloc]init];
__block NSMutableArray* annots = [[NSMutableArray alloc]init];
PFQuery* query = [PFQuery queryWithClassName:@"malls"];
[query getObjectInBackgroundWithId:@"Eaib9yfTRe" block:^(PFObject *object, NSError *error) {
data = [object objectForKey:@"mallsData"];
annots = [self createAnnotations:data];
}];
return annots;
}
问题是getObjectInBackground是异步的,总是在从服务器获取数据之前返回。 我尝试在代码块内移动“返回注释”,但出现以下错误:“不兼容的块指针类型”。
我将5个“购物中心”对象上传到“ malls2”类。 每个对象都有2个属性-名称和地址:
for(int i = 0; i < 5; i++) { PFObject *mallsObj = [PFObject objectWithClassName:@"malls2"]; mallsObj[name] = [[self.malls objectAtIndex:i]objectForKey:name]; mallsObj[address] = [[self.malls objectAtIndex:i]objectForKey:address]; [mallsObj saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) { if(succeeded) NSLog(@"yay"); else NSLog(@"%@", error.description); }];
}
然后我尝试找回它:
-(NSMutableArray *)createAnnotationsFromParse
{
__block Annotation* anno = [[Annotation alloc]init];
__block NSMutableArray* annots = [[NSMutableArray alloc]init];
PFQuery* query = [PFQuery queryWithClassName:@"malls2"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if(error)
NSLog(@"%@", error.description);
else
{
for(int i = 0; i < [objects count]; i++)
{
//createAnnotationWithTitle is a func in a different class that creates the annotation
anno = [anno createAnnotationWithTitle:[[objects objectAtIndex:i] objectForKey:name] andAddress:[[objects objectAtIndex:i]objectForKey:address]];
}
[annots addObject:anno];
}
}];
return annots;
}
我得到5个对象,但它们都空了。