2013-06-04 14 views
0

守則,我使用進入後臺後,插入數據到SQLite數據庫如下如何使用Objective-C的

- (void)applicationDidEnterBackground:(UIApplication *)application 
{ 
    [self performSelector:@selector(addContacts) withObject:nil afterDelay:0.1]; 
} 

-(void)addContacts 
{ 
    sqlite3_stmt *statement; 

    const char *dbpath = [databasePath UTF8String]; 

    if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK) 
    { 
     NSString *insertSQL = [NSString stringWithFormat: 
           @"INSERT INTO CONTACTS (name, address, phone) VALUES (\"%@\", \"%@\", \"%@\")", 
           @"Anu", @"0009", @"Hyderabad"]; 

     const char *insert_stmt = [insertSQL UTF8String]; 
     sqlite3_prepare_v2(contactDB, insert_stmt, 
          -1, &statement, NULL); 
     if (sqlite3_step(statement) == SQLITE_DONE) 
     { 


     } 
     else { 

      } 
     sqlite3_finalize(statement); 
     sqlite3_close(contactDB); 
    } 

else 
{ 
    UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Information Alert" 
                 message:@"Please enter the name " 
                delegate:nil 
              cancelButtonTitle:@"OK" 
              otherButtonTitles:nil]; 
    [message show]; 
} 
} 

但它不工作時,它在後臺

回答

0

你可以做技術上通過啓用後臺任務。但是,蘋果只允許某些任務的背景模式,例如位置服務,音樂,voip等。如果您僞造音樂或其他允許的任務來執行此操作,您的應用可能會被拒絕。

+0

是否有任何方法在後臺運行數據庫 –

0

你可以告訴iOS你打算執行一個短暫的後臺任務,沒有任何額外的背景模式。完成後請使用UIApplication的beginBackgroundTaskWithExpirationHandler:,然後撥打endBackgroundTask:。 iOS不保證您將被允許執行後臺任務。有關更多信息,請參閱這些方法的文檔。

+0

Jesper beginBackgroundTaskWithExpirationHandler用於在應用程序進入後臺後繼續運行方法我想在進入後臺後10秒將數據插入數據庫 –

+0

是的,so使用該方法,然後啓動一個計時器10秒鐘,然後執行所需的操作。 – Jesper