我正在使用Parse数据库(异步)创建一个iOS应用程序来存储填充mapview时将使用的信息。我一直试图找出问题很长一段时间,并做了大量的研究,没有任何运气。但是,我找到了问题的根源。
在我的代码中,我正在查询解析数据库,希望获得我想要的信息,然后将信息存储在自定义的pointAnnotation类中,该类的类型为MkPointAnnotation。每个项目都存储在pointAnnotations数组中,一旦数据库中的所有项目都存储在数组中,注释就会添加到MyMapView中。 - 我尝试在创建时添加注释,这不会改变任何内容。
我遇到的问题是随机的,查询将在for(供应商中的PFObject *供应商)下进行迭代并出现错误,调用NSLog(@"%@",error.debugDescription );在输出日志中显示(null)。每次运行应用程序时,返回null的对象数量似乎都会发生变化,偶尔会按预期工作。添加do while(pointArray.count< query.countObjects)后,该函数将迭代大约20-30次,然后将添加正确数量的注释,但是效率非常低。
这是Parse中的低效率还是有更好的方法来实现预期结果?
PFQuery *query = [PFQuery queryWithClassName:@"Vendors"];
[query orderByDescending:@"updatedAt"];
[query findObjectsInBackgroundWithBlock:^(NSArray *vendors, NSError *error){
NSMutableArray *pointArray = [[NSMutableArray alloc] init];
if (!error) {
// The find succeeded.
// Do something with the found objects
do {
pointArray = [[NSMutableArray alloc] init];
for (PFObject *vendor in vendors) {
NSDate *lastUpdated = vendor.updatedAt;
NSDate *today = [NSDate date];
NSDate *newDate = [lastUpdated dateByAddingTimeInterval:86400];
if (today <= newDate) {
PFGeoPoint *point = vendor[@"Location"];
NSString *vendor_ID = vendor[@"Vendor_ID"];
NSMutableArray *FruitList = vendor[@"Fruits"];
NSMutableArray *VeggieList = vendor[@"Veggies"];
NSMutableArray *addressArray = vendor[@"Address"];
NSString *startHr = vendor[@"Start_Time"];
NSString *endHr = vendor[@"End_Time"];
Boolean more = false;
NSString *moreString = vendor[@"And_More"];
if ([moreString isEqual: @"true"]) {
more = true;
}
CLLocationCoordinate2D location;
location.latitude = point.latitude;
location.longitude = point.longitude;
pointAnnotation *newAnnotation = [[pointAnnotation alloc] init];
if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"language"] isEqual:@"ENGLISH"]){
FindCartsLabel.text = @"Find Carts within:";
MilesTextField.text = @"Show All";
milesArray=[[NSArray alloc]initWithObjects:@"Show All", @"1 Mile", @"5 Miles", @"10 Miles", @"20 Miles", nil];
AddressBar.placeholder = ENGLISH_Address;
newAnnotation.title = @"Good. To. Go. Vendor";
newAnnotation.fruits = FruitList;
newAnnotation.veggies = VeggieList;
}else if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"language"] isEqual:@"SPANISH"]){
FindCartsLabel.text = @"Encuentra Carros Dentro:";
newAnnotation.title = @"Good. To. Go. Vendedor";
AddressBar.placeholder = SPANISH_Address;
NSMutableArray *spanishFruitList = [[NSMutableArray alloc]init];
for (NSString *current in FruitList) {
MilesTextField.text = @"Mostrar Todo";
milesArray=[[NSArray alloc]initWithObjects:@"Mostrar Todo", @"1 Milla", @"5 Millas", @"10 Millas", @"20 Millas", nil];
if ([current isEqual:@"Apples"]) {
[spanishFruitList addObject:SPANISH_Apples];
}
if ([current isEqual:@"Bananas"]) {
[spanishFruitList addObject:SPANISH_Bananas];
}
if ([current isEqual:@"Citrus"]) {
[spanishFruitList addObject:SPANISH_Citrus];
}
if ([current isEqual:@"Mangos"]) {
[spanishFruitList addObject:SPANISH_Mangos];
}
if ([current isEqual:@"Strawberries"]) {
[spanishFruitList addObject:SPANISH_Strawberries];
}
if ([current isEqual:@"And More"]) {
[spanishFruitList addObject:SPANISH_More];
}
}
NSMutableArray *spanishVeggieList = [[NSMutableArray alloc]init];
for (NSString *current in VeggieList) {
if ([current isEqual:@"Avocados"]) {
[spanishVeggieList addObject:SPANISH_Avocados];
}
if ([current isEqual:@"Broccoli"]) {
[spanishVeggieList addObject:SPANISH_Broccoli];
}
if ([current isEqual:@"Carrots"]) {
[spanishVeggieList addObject:SPANISH_Carrots];
}
if ([current isEqual:@"Squash"]) {
[spanishVeggieList addObject:SPANISH_Squash];
}
if ([current isEqual:@"Onions"]) {
[spanishVeggieList addObject:SPANISH_Onions];
}
if ([current isEqual:@"Tomatoes"]) {
[spanishVeggieList addObject:SPANISH_Tomatoes];
}
if ([current isEqual:@"And More"]) {
[spanishVeggieList addObject:SPANISH_More];
}
}
newAnnotation.fruits = spanishFruitList;
newAnnotation.veggies = spanishVeggieList;
}
newAnnotation.coordinate = location;
newAnnotation.vendorID = vendor_ID;
newAnnotation.startHour = startHr;
newAnnotation.endHour = endHr;
newAnnotation.loc = point;
newAnnotation.isCustomAddress = false;
//newAnnotation.subtitle = address;
__block NSString *address = [NSString stringWithFormat:@"%@ %@, %@, %@, %@",
addressArray[0], addressArray[1],
addressArray[2], addressArray[3],
addressArray[4]];
__block NSString *currAddress = [NSString stringWithFormat:@"%@ %@\n"
"%@, %@, %@\n"
"%@\n",
addressArray[0], addressArray[1],
addressArray[2], addressArray[3],
addressArray[4], addressArray[5]];
newAnnotation.subtitle = address;
newAnnotation.addressFormatted = currAddress;
static NSString *identifier = @"MyLocation";
MKPinAnnotationView *currentView = [[MKPinAnnotationView alloc] initWithAnnotation:newAnnotation reuseIdentifier:identifier];
[pointArray addObject:currentView];
} else {
//[self viewDidLoad];
NSLog(@"%@", error.debugDescription);
}
//} ];
}
} while (pointArray.count < query.countObjects);
}
if (pointArray.count == query.countObjects) {
for (MKPinAnnotationView *currentPoint in pointArray) {
[self.MyMapView addAnnotation:currentPoint.annotation];
}
}
}];
提前感谢您的帮助。我真的不明白为什么这段代码只能在一次迭代后才能完成。
答案 0 :(得分:0)
NSLog(@&#34;%@&#34;,error.debugDescription);看起来不是在正确的地方。它位于与if(今天&lt; = newDate)相关联的else块中,该块位于代码块内,只有当error为null时才会执行,这就是为什么它在日志中显示为null(当它是什么时真正的意思是&#34;今天&gt; newDate&#34;)。 - 安娜