Search code examples
sqliteasynchronousxml-parsingios7nsnotifications

How to parse xml and simultaneously save to sqlite asynchronously in iOS 7?


I am parsing XML Document by using TBXML but I have to Parse XML and store data to SQLite asynchronously with notifications [that is Parsing and storing data in SQLite]. Please help me to overcome this problems. Thanks in advance....


Solution

  • For that You can use NSNotificationCenter and GCD,

    First set NSNotificationCenter for your process using,

    [[NSNotificationCenter defaultCenter] addObserver:self 
                                                 selector:@selector(dataStore) 
                                                     name:@"dataStoreComplete" object:nil];
    
    
    - (void)dataStore
    {
        NSLog(@"Received Notification - Data stored in databse");
    }
    

    You GCD for parsing and storing in database

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    
        // switch to a background thread and perform your expensive operation
     //  parse and store all data in sqlite,
    
    
        dispatch_async(dispatch_get_main_queue(), ^{
            // switch back to the main thread to update your UI
     [[NSNotificationCenter defaultCenter] postNotificationName:@"dataStoreComplete" object:nil]; 
        });
    });