添加解析查询返回内的MapView注释随机空 [英] Adding mapview annotations within parse query returns null randomly

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

问题描述

我创建使用解析数据库(异步)来存储填充图形页面时将使用的信息的iOS应用。我一直在试图找出什么是错的很长一段时间,并已做过大量的研究没有任何运气。我有,但是发现了问题的根源。

在我的code,我在得到我想要的信息,然后保存在自定义pointAnnotation类,它的类型是MkPointAnnotation的信息查询希望解析数据库。每个项目中存储pointAnnotations的阵列,并且曾经在数据库中的所有项都被存储在数组中,所述注释被添加到MyMapView。 - 我已经尝试添加注释,因为它们是创造,它不会改变任何东西。

我一直有这个问题是随机,查询将根据迭代的(PFObject *在供应商的供应商),并达到一个错误,调用的NSLog(@%@,error.debugDescription);这显示(空)的输出日志。对象返回空值的金额都好像我运行应用程序时间来改变,偶尔预期它会工作。增加一个做,而(pointArray.count< query.countObjects)后,功能将遍历大约20-30次,然后将添加注释的正确数目,但是,这是非常低效的。

这是中解析效率低下或者有没有更好的方式来达到预期的效果?

  PFQuery *查询= [PFQuery queryWithClassName:@供应商];
    [查询orderByDescending:@updatedAt];    [查询findObjectsInBackgroundWithBlock:^(* NSArray的厂商,NSError *错误){
        NSMutableArray里* pointArray = [[NSMutableArray里的alloc]初始化];        如果(!错误){
            //这一发现成功。
            //使用已找到对象的东西
            做{
                pointArray = [[NSMutableArray里的alloc]初始化];            对于(PFObject *在供应商的供应商){
                *的NSDate = LASTUPDATED vendor.updatedAt;
                * NSDate的今天= [NSDate的日期];
                *的NSDate = newDate [LASTUPDATED dateByAddingTimeInterval:86400]。                如果(今< = newDate){                    PFGeoPoint *点=供应商[@位置];
                    * NSString的= VENDOR_ID供应商[@VENDOR_ID];
                    NSMutableArray里* FruitList =供应商[@水果];
                    NSMutableArray里* VeggieList =供应商[@素食者];
                    NSMutableArray里* addressArray =供应商[@地址];
                    * NSString的= startHr供应商[@START_TIME];
                    * NSString的= endHr供应商[@END_TIME];
                    布尔多= FALSE;                    * NSString的=了解详情STRING供应商[@And_More];
                    如果([了解详情STRING的isEqual:@真]){
                        更多= TRUE;
                    }
                    CLLocationCoordinate2D位置;                    location.latitude = point.latitude;
                    location.longitude = point.longitude;                    pointAnnotation * newAnnotation = [[pointAnnotation的alloc]初始化];
                    如果([[[NSUserDefaults的standardUserDefaults] objectForKey:@语言]的isEqual:@ENGLISH]){
                        FindCartsLabel.text = @查找范围内车;
                        MilesTextField.text = @全部显示;
                        milesArray = [[NSArray的页头] initWithObjects:@全部显示,@1英里,@5万里,@10英里,@20英里零]
                        AddressBar.placeholder = ENGLISH_Address;
                        newAnnotation.title = @。好去供应商。
                        newAnnotation.fruits = FruitList;
                        newAnnotation.veggies = VeggieList;
                    }否则如果([[[NSUserDefaults的standardUserDefaults] objectForKey:@语言]的isEqual:@西班牙]){
                        FindCartsLabel.text = @Encuentra卡罗迪登特鲁:
                        newAnnotation.title = @好走Vendedor。
                        AddressBar.placeholder = SPANISH_Address;                        NSMutableArray里* spanishFrui​​tList = [[NSMutableArray里的alloc]初始化];
                        对于(的NSString *在FruitList电流){
                            MilesTextField.text = @Mostrar TODO;
                            milesArray = [[NSArray的页头] initWithObjects:@藤堂Mostrar,@1米拉​​,@5 Millas,@10 Millas,@20 Millas零]
                            如果([当前的isEqual:@苹果]){
                                [spanishFrui​​tList ADDOBJECT:SPANISH_Apples];
                            }
                            如果([当前的isEqual:@香蕉]){
                                [spanishFrui​​tList ADDOBJECT:SPANISH_Bananas];
                            }
                            如果([当前的isEqual:@柑橘]){
                                [spanishFrui​​tList ADDOBJECT:SPANISH_Citrus];
                            }
                            如果([当前的isEqual:@芒果]){
                                [spanishFrui​​tList ADDOBJECT:SPANISH_Mangos];
                            }
                            如果([当前的isEqual:@草莓]){
                                [spanishFrui​​tList ADDOBJECT:SPANISH_Strawberries];
                            }
                            如果([当前的isEqual:@多]){
                                [spanishFrui​​tList ADDOBJECT:SPANISH_More];
                            }
                        }
                        NSMutableArray里* spanishVeggieList = [[NSMutableArray里的alloc]初始化];
                        对于(的NSString *在VeggieList电流){
                            如果([当前的isEqual:@鳄梨]){
                                [spanishVeggieList ADDOBJECT:SPANISH_Avocados];
                            }
                            如果([当前的isEqual:@西兰花]){
                                [spanishVeggieList ADDOBJECT:SPANISH_Broccoli];
                            }
                            如果([当前的isEqual:@胡萝卜]){
                                [spanishVeggieList ADDOBJECT:SPANISH_Carrots];
                            }
                            如果([当前的isEqual:@南瓜]){
                                [spanishVeggieList ADDOBJECT:SPANISH_Squash];
                            }
                            如果([当前的isEqual:@洋葱]){
                                [spanishVeggieList ADDOBJECT:SPANISH_Onions];
                            }
                            如果([当前的isEqual:@西红柿]){
                                [spanishVeggieList ADDOBJECT:SPANISH_Tomatoes];
                            }
                            如果([当前的isEqual:@多]){
                                [spanishVeggieList ADDOBJECT:SPANISH_More];
                            }
                        }                        newAnnotation.fruits = spanishFrui​​tList;
                        newAnnotation.veggies = spanishVeggieList;
                    }                    newAnnotation.coordinate =位置;
                    newAnnotation.vendorID = VENDOR_ID;
                    newAnnotation.startHour = startHr;
                    newAnnotation.endHour = endHr;
                    newAnnotation.loc =点;
                    newAnnotation.isCustomAddress = FALSE;                    //newAnnotation.subtitle =地址;
                    __block的NSString *地址= [的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 =地址;                    newAnnotation.addressFormatted = currAddress;
                    静态的NSString *标识符= @MyLocation
                    MKPinAnnotationView * currentView = [[MKPinAnnotationView页头] initWithAnnotation:newAnnotation reuseIdentifier:符];
                    [pointArray ADDOBJECT:currentView];                }其他{
                    // [个体经营的viewDidLoad];
                    的NSLog(@%@,error.debugDescription);
                }
                //}];            }
        }而(pointArray.count< query.countObjects);        }
        如果(pointArray.count == query.countObjects){
            对于(MKPinAnnotationView * currentPoint在pointArray){
                [self.MyMapView addAnnotation:currentPoint.annotation];
            }
        }    }];

在此先感谢您的帮助。我真的不明白为什么code不会只有一个迭代后完成。


解决方案

  

本的NSLog(@%@,error.debugDescription);看起来并不像它在正确的地方。它在与该如果相关的其他块(今< = newDate),这是code的块时,才会执行里面,如果误差为null这就是为什么说空在日志中(当它真正手段是当今> newDate)。 - 安娜


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.

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.

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.

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.

解决方案

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

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

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