0

I'm loading data in my ViewController inside of an AFNetworking request. However doing this causes the data inside of that method to load 2-3 seconds after everything else inside of my viewDidLoad has already loaded. WHY? How can I make it so that this data all appears on the screen at the same time? I've seen a few variations of this question on stack and none of the suggested fixes seem to work for me. See code below.

ViewController.m

 NSMutableDictionary *viewParams = [NSMutableDictionary new];
    [viewParams setValue:@"map" forKey:@"view_name"];
    [DIOSView viewGet:viewParams success:^(AFHTTPRequestOperation *operation, id responseObject) {

        dispatch_async(dispatch_get_main_queue(), ^{
        self.mapuserData = (NSMutableArray *)responseObject;

        [self.tableView reloadData];


        [operation responseString];

        if ([self.mapuserData count] > 0 ) {

                NSString *thisUserId = [self.mapuserData objectForKey:@"users_name"];


                NSPredicate *predicate = [NSPredicate predicateWithFormat:@"node_title CONTAINS[cd] %@",
                                          thisUserId];

                NSArray *resultArray = [self.acceptedFriends filteredArrayUsingPredicate:predicate];
                NSLog(@"RESULT %@", resultArray);

                if ([resultArray count] > 0) {
                    NSLog(@"Executed!");
                    self.addFriend.hidden = YES;
                    self.orangeFriendCircle.hidden = YES;
                } else {

                    self.addFriend.hidden = NO;
                    self.orangeFriendCircle.hidden = NO;

                }

                self.username.text = self.mapuserData[@"users_name"];

                self.userBio.text = self.mapuserData[@"userbio"];

                NSString *thirdLink = self.mapuserData[@"photo_path"];

                NSString *ImageURLTwo = thirdLink;
                NSData *imageDataTwo = [NSData dataWithContentsOfURL:[NSURL URLWithString:ImageURLTwo]];

                self.userPhoto.image = [[UIImage alloc] initWithData:imageDataTwo];

                self.supervisionLabel.text = [self.mapuserData objectForKey:@"supervision"];

                self.propertyLabel.text = [self.mapuserData objectForKey:@"property type"];

                self.childrenLabel.text = [self.mapuserData objectForKey:@"children"];

                self.specialLabel.text = [self.mapuserData objectForKey:@"special skills"];

                self.emergencyLabel.text = [self.mapuserData objectForKey:@"emergency"];

                self.username.text = [self.mapuserData objectForKey:@"first name"];

                self.userBio.text = [self.mapuserData objectForKey:@"userbio"];
        });

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Failure: %@", [error localizedDescription]);
    }];
Brittany
  • 1,359
  • 4
  • 24
  • 63
  • 2
    You are making an asynchronous request i.e using a separate thread to fetch data. If you dont want that, you can fetch the data on previous ViewControler, and once you get it you initiaize your Viewcontroller with data and push/present it on the screen. – Puneet Sharma Sep 05 '17 at 05:36
  • 2
    Exactly; It's an **asynchronous** operation. That means that your `-viewDidLoad` method requests the operation be started on the background, by a **separate thread**, and goes on with its life without waiting for the operation to complete. You can't force an external, lengtht operation (network, server) to finihs in time, and you aren't allowed to stall the main (UI) thread. – Nicolas Miari Sep 05 '17 at 05:39
  • 3
    That's why **progress indicators** exist. But ideally (as suggested by @PuneetSharma), you would somehow anticipate your user and **preload** the data _before_ they navigate to htat screen. – Nicolas Miari Sep 05 '17 at 05:41
  • I was afraid that might be the answer @PuneetSharma. Ugh. So to be clear: There's no way to fetch data from my server on the SAME controller it needs to be displayed on if I want it displayed instantly?? That seems cruel... – Brittany Sep 05 '17 at 05:42
  • @Brittany: fetching data from server is expensive, and hence is performed in another thread(other from main thread). But yeah there are ways to do it in the main thread as well(not preferred as it will block any interaction from user until your main thread is free). I guess you are using AFNetworking for sending requests to network. You can look at this thread https://stackoverflow.com/questions/7969865/can-afnetworking-return-data-synchronously-inside-a-block. But again that is not preferred and not done in majority of the cases. – Puneet Sharma Sep 05 '17 at 05:48
  • Are you expecting things to load INSTANTLY from an external server? Is there any app able to do that? – Tj3n Sep 05 '17 at 05:48
  • 1
    Haha good point - I don't want instant, I just want the illusion of instant. BUT, @PuneetSharma, did as you suggested, works like a charm. THANK YOU :) – Brittany Sep 05 '17 at 05:51

0 Answers0