Cannot get values from API when called simultaneously in TabViewController 1 & 2 - Objective C

29 Views Asked by At

I have a TabViewController with 2 tabs, The first tab is my Homescreen. When I open my App and the first tab shows it calls an API.

API CALLED

_loadingHistory = ([(AppDelegate *)[[UIApplication sharedApplication] delegate] updateInprogress]);
    if(([NetworkConnectivity hasConnection]) && ([(AppDelegate *)[[UIApplication sharedApplication] delegate] datasourceUpdateRequired]) && !([(AppDelegate *)[[UIApplication sharedApplication] delegate] updateInprogress]))
    {
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            _loadingHistory = YES;
            [(AppDelegate *)[[UIApplication sharedApplication] delegate] setUpdateInprogress:YES];
            [self startOperationsForReferenceList];
            GetMostRecentEnquiryController *getRecent = [[GetMostRecentEnquiryController alloc] initWithPelicanId:[[NSUserDefaults standardUserDefaults] valueForKey:needId] NumberOfItems:100 Delegate:self];
            [getRecent fetchMostRecentData];

            if (!self.loadingHistory) {
                [self loadMostRecentHistory];
            }
        });
    } else {
        if (!self.loadingHistory) {
            [self loadMostRecentHistory];
        }
    }

I have a MutableArray that's gonna receive the result of the API. Supposedly after the API call, it must return the set of values to me and be placed in the MutableArray I created. In my first tab it works well. The second tab pretty much does the same it calls the same API.

But, when I launch the screen(shows 1st tab), immediately clicks on 2nd tab (while 1st tab is loading, meaning the API is not yet finished), the 2nd tab loads forever. But when I click again on the first tab and return to 2nd tab, the details are there.

FETCH MOST RECENT DATA METHOD

if(![NetworkConnectivity hasConnection]) return;
ShowNetworkActivityIndicator();
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"www.URI.com"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:[NSString stringWithFormat:@"%d",[self numberOfHistoryItems]] forKey:@"numbertofetch"];
[request setPostValue:[self neededId] forKey:@"neededId"];
[request setPostValue:@"commencement" forKey:@"sortby"];
[request setDelegate:self];
[request startSynchronous];
if(request.responseStatusCode == 200)
{
    [self.listOfAllItems removeAllObjects];
    [self.listOfFutureItems removeAllObjects];
    [self clearDatabase];
    NSData *jsonData = [request responseData];
    id result = [jsonData objectFromJSONData];
    HelloGetMostRecentEnquiries *userBase = [[HelloGetMostRecentEnquiries alloc] initWithDictionary:result];
    if([userBase isSuccess])
    {
        for (int i=0; i < userBase.enquiries.count; i++)
        {
            HelloEnquiries *enq = [[userBase enquiries] objectAtIndex:i];
            [self saveItemsToDatabase:enq];
        }
    }
    [(NSObject *)self.delegate performSelectorOnMainThread:@selector(finishedGetMostRecent:) withObject:self waitUntilDone:NO];
    HideNetworkActivityIndicator();
}
else
{
    HideNetworkActivityIndicator();
}
0

There are 0 best solutions below