在解析查询中添加地图视图注释随机返回空值 [英] Adding mapview annotations within parse query returns null randomly

查看:24
本文介绍了在解析查询中添加地图视图注释随机返回空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am creating an iOS app using Parse database(asynchronously) to store information that will be used when populating a mapview. I have been trying to figure out what is wrong for a long time and have done plenty of research without any luck. I have, however, found the source of the issue.

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

In my code, I am querying the parse database in hopes of getting the information I want and then storing the information in a custom pointAnnotation class, which is of type MkPointAnnotation. Each item is stored in an array of pointAnnotations, and once all items in the database have been stored in the array, the annotations are added to MyMapView. --I have tried adding the annotations as they are created, which does not change anything.

我遇到的问题是随机地,查询将在 for(PFObject *vendor in Vendors) 下迭代并达到错误,调用 NSLog(@"%@", error.debugDescription);在输出日志中显示 (null).每次运行应用程序时,返回 null 的对象数量似乎都会发生变化,有时它会按预期工作.添加 do while(pointArray.count < query.countObjects) 后,该函数将迭代大约 20-30 次,然后添加正确数量的注释,但效率极低.

The issue I have been having is that randomly, the query will iterate under the for(PFObject *vendor in Vendors) and reach an error, calling NSLog(@"%@", error.debugDescription); which shows (null) in the output log. The amount of objects that return null seems to change each time I run the application, and occasionally it will work as expected. After adding a do while(pointArray.count < query.countObjects), the function will iterate roughly 20-30 times and then will add the correct number of annotations, however, it is extremely inefficient.

这是 Parse 中的低效问题还是有更好的方法来实现预期结果?

Is this an inefficiency within Parse or is there a better way to achieve the expected results?

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];
            }
        }

    }];

在此先感谢您的帮助.我真的不明白为什么这段代码只经过一次迭代就无法完成.

Thanks in advance for the help. I do not really understand why this code would not complete after only one iteration.

推荐答案

NSLog(@"%@", error.debugDescription);看起来不像是在正确的地方.它位于与 if (today <= newDate) 关联的 else 块中,该块位于代码块中,该代码块仅在 error 为 null 时才执行,这就是为什么它在日志中显示 null(当它真正的意思是今天 > 新日期").——安娜

The NSLog(@"%@", error.debugDescription); doesn't look like it's in the right place. It's in an else block that is associated with the if (today <= newDate) which is inside a block of code that is only executed if error is null which is why it says null in the log (when what it really means is "today > newDate"). – Anna

这篇关于在解析查询中添加地图视图注释随机返回空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆