在parsing查询中添加mapview注释将随机返回null

我正在使用Parse数据库(asynchronous)创build一个iOS应用程序来存储将在填充mapview时使用的信息。 我一直在努力弄清楚什么是错误的,并且没有任何运气做了大量的研究。 但是,我发现了这个问题的根源。

在我的代码中,我正在查询parsing数据库,希望获取我想要的信息,然后将信息存储在types为MkPointAnnotation的自定义pointAnnotation类中。 每个项目都存储在一个pointAnnotations数组中,一旦数据库中的所有项目都存储在数组中,则注释将被添加到MyMapView。 – 我尝试添加注释,因为它们被创build,这不会改变任何东西。

我遇到的问题是,随机查询将在for(PFObject * vendor in Vendors)下进行迭代,并出现错误,调用NSLog(@“%@”,error.debugDescription); 在输出日志中显示(空)。 每次运行应用程序时,返回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]; } } }]; 

先谢谢您的帮助。 我不明白为什么这个代码只有一次迭代后才能完成。

NSLog(@“%@”,error.debugDescription); 看起来不像是在正确的地方。 它在一个与if(today <= newDate)关联的else块中,它是在一个代码块内部执行的,只有在error为null的情况下才会执行,这就是为什么它在日志中表示为null(当它的意思是“今天> newDate“)。 – 安娜