在解析查询中添加 mapview 注释会随机返回 null

Posted

技术标签:

【中文标题】在解析查询中添加 mapview 注释会随机返回 null【英文标题】:Adding mapview annotations within parse query returns null randomly 【发布时间】:2014-05-10 19:50:13 【问题描述】:

我正在使用 Parse 数据库(异步)创建一个 ios 应用程序来存储将在填充地图视图时使用的信息。很长一段时间以来,我一直试图找出问题所在,并且在没有任何运气的情况下进行了大量研究。不过,我已经找到了问题的根源。

在我的代码中,我正在查询解析数据库,希望得到我想要的信息,然后将信息存储在自定义 pointAnnotation 类中,该类是 MkPointAnnotation 类型。每个项目都存储在一个pointAnnotations数组中,一旦数据库中的所有项目都存储在数组中,注释就会添加到MyMapView。 --我已经尝试在创建注释时添加它们,这不会改变任何东西。

我遇到的问题是,随机查询将在 for(PFObject *vendor in Vendors) 下迭代并遇到错误,调用 NSLog(@"%@", error.debugDescription);在输出日志中显示 (null)。每次我运行应用程序时,返回 null 的对象数量似乎都会发生变化,有时它会按预期工作。添加 do while(pointArray.count

这是 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 &lt;= newDate) 关联的else 块中,if (today &lt;= newDate) 位于 inside 代码块中,仅当error 为 null 时才会执行,这就是它在日志中显示 null 的原因(当真正的意思是“今天 > 新日期”)。 error 的 NSLog 应该更远——可能就在 if (pointArray.count == query.countObjects) 之前。 不相关 但是:您应该只在 viewForAnnotation 委托方法中创建 MKPinAnnotationView 对象。在这段代码中,您应该将pointAnnotation 对象本身添加到pointArray,然后最后您可以执行[self.MyMapView addAnnotations:pointArray];(无需循环)。 哇,我不敢相信我错过了。非常感谢。 【参考方案1】:

NSLog(@"%@", error.debugDescription);看起来它不在正确的位置。它位于与 if (today 新日期”)。 ——安娜

【讨论】:

以上是关于在解析查询中添加 mapview 注释会随机返回 null的主要内容,如果未能解决你的问题,请参考以下文章

使用两个功能将注释添加到 mapView

群集注释图钉需要更多时间才能在 Mapview 上添加它们

如何检测在 MapView 中选择了哪个注释

每次移动 UISlider 时删除注释 - mapView, Swift

如何更改 MapView 注释中的标注气泡颜色?

在 Swift 中的同一个 mapView 中分配多个注释 pinColors?