Boobalan
Boobalan

Reputation: 53

Fetching data using NsoperationQueue in sleep mode

I have added data fetching operations to NSOperationQueue .During the process, I just put the device to sleep mode and the process gets stopped. I surfed stack overflow an get some basic ideas.I need help to continue my fetching process without any interruption when device moves to sleep mode.Help appreciated !!


    NSManagedObjectContext *managedObjectContext=((AppDelegate *)[[UIApplication sharedApplication] delegate]).managedObjectContext;

    NSOperationQueue *downLoadQueue=((AppDelegate *)[[UIApplication sharedApplication] delegate]).downloadqueue;

//Fetch operation

        if([fetchqueue count]>0)
        {
            Queue *queue=[fetchqueue objectAtIndex:0];

            queue.status=@"INP";
            [managedObjectContext performBlockAndWait:^{
                NSError * error = nil;
                if (![managedObjectContext save:&error]){
                    NSLog(@"Unresolved error while loading3");

                }
            }];

            DownloadOperation *downloadOp=[[DownloadOperation alloc]init];
            downloadOp.queue=queue;
            [downLoadQueue addOperation:downloadOp];

        }
    }


The downLoadQueue started its execution. It is fetching data from server meanwhile the device goes to sleep and the execution stops. I don't know how to continue in this block applicationDidEnterBackground. How can I get my lengthy downloading process during sleep mode?


Also tried this one..But the operation was not resumed.

//code follows
- (void)applicationDidEnterBackground:(UIApplication *)application { // Start the long-running task and return immediately. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        NSOperationQueue *downLoadQueue;
        downLoadQueue=((AppDelegate *)[[UIApplication sharedApplication] delegate]).downloadqueue;

        [downLoadQueue waitUntilAllOperationsAreFinished]; 

    });
}

Please suggest any ideas.

Upvotes: 0

Views: 173

Answers (1)

evnaz
evnaz

Reputation: 190

I think the only way to do this is background task. Take a look at apple docs about this. There are also a lot of answers on stackowerflow how to implemet such feature.

I think in your case you can try next code to execute NSOperations a in backgraund task:

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    bgTask = [application beginBackgroundTaskWithName:@"MyTask" expirationHandler:^{
        // Clean up any unfinished task business by marking where you
        // stopped or ending the task outright.
        [application endBackgroundTask:bgTask];
        bgTask = UIBackgroundTaskInvalid;
    }];

    // Start the long-running task and return immediately.

    //Start your NSOperationQueue if it's not executing
    //Lock current thread while operations are executing
    [queue waitUntilAllOperationsAreFinished]; 

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        // Do the work associated with the task, preferably in chunks.

        [application endBackgroundTask:bgTask];
        bgTask = UIBackgroundTaskInvalid;
    });
}

Upvotes: 0

Related Questions