0

I have a DataManager class that is responsible for fetching data and handing it over to the DatabaseManager, which in-turn will insert it into core data.

The method that exposes itself to the webservice is below

-(void)fetchDetailsForId:(NSString *)userId withFieldInformation:(NSString *)fieldInfo
{
    if (SessionIsActive) {
        [APIRequester startWithRequest:[NSString stringWithFormat:@"%@?%@",userId, fieldInfo]
completionHandler:^(APIConnectionManager *connection, id user, NSError *error) {
            //This is where the results are returned and the API manages its own threads and returns the result lazily
        }];
    }
}

The above method is within the DataManager class. Now, a few methods in the same class call the above method to get data from the server. This fetched data is then forwarded to the DatabaseManager for inserting into core data. A sample of this is

-(void)fetchCurrentDataForLoggedInUser
{
    NSData *fetchedData = [self fetchDetailsForId:loggedInId withFieldInformation:@"all"];
        //the fetchedData is then forwarded to DatabaseManager
}

Now, since the web method (first method) gets the data in the background thread (managed by the API), the value of the "fetchedData" in the above method will be null since the web method exits before the API gets the relevant data.

Can someone tell me the most recommended way of handling a situation like this? I am not asking for sample code or anything, just the right direction should be enough. I am looking for a permanent solution than a hack or easy workaround.

Thank you

iSee
  • 604
  • 1
  • 14
  • 31
  • 1
    Use blocks. Or use a framework like RestKit which will probably do 95% of the work for you. – Wain Jun 17 '13 at 10:14

1 Answers1

0

Make a property for NSData says fetchedData. in .h and synthesize it in .m

then set this property after date fetch then call the method which is required. There are some delegate which tells you data data loading is done so at there you can set your property an from background you can call UI updating method on main thread that gives you your desired result.

you can use selector like this for calling on main thread

[self performSelectorOnMainThread:@selector(rollBar:)
      withObject:nil
      waitUntilDone:false];
Ishu
  • 12,797
  • 5
  • 35
  • 51
  • This is not the exact thing I was looking for but gave me an idea of using custom delegate for maintaining the stack of calls (hence accepting the answer). I hope I am on the right course with this. – iSee Jun 17 '13 at 10:44