2012-10-18 40 views
0

我有超過5000記錄從服務器加載到我的應用程序並插入到數據庫。 在插入一些成功插入這些記錄時,我得到這種錯誤,我的應用程序崩潰。數據庫格式錯誤在iphone +錯誤磁盤I/O錯誤

數據庫在iPhone +錯誤的磁盤I/O錯誤

畸形以下是代碼片段,可能是有用的,在這裏,我通過一個讓唱片在5個類別中的一個,並將其插入到數據庫中

if (tag==1) { 
     NSMutableDictionary *catDic=[dic valueForKeyPath:@"GetAllRecords.results.categories.category"]; 
     [self performSelectorInBackground:@selector(insertDataInCategoryMaster:) withObject:catDic]; 

     NSMutableDictionary *levelDic=[dic valueForKeyPath:@"GetAllRecords.results.levels.level"]; 
     [self performSelectorInBackground:@selector(insertDataInLevelMaster:) withObject:levelDic]; 

     NSMutableDictionary *queDic=[dic valueForKeyPath:@"GetAllRecords.results.questions.question"]; 

     [self performSelectorInBackground:@selector(insertDataInQueMaster:) withObject:queDic]; 

     [self getQueCatLevelData:2]; 
    } 
    else if(tag==2){ 

     NSMutableDictionary *queDic=[dic valueForKeyPath:@"GetAllRecords.results.questions.question"]; 
     [self performSelectorInBackground:@selector(insertDataInQueMaster:) withObject:queDic]; 

     [self getQueCatLevelData:3]; 

    } 
    else if(tag==3){ 

     NSMutableDictionary *queDic=[dic valueForKeyPath:@"GetAllRecords.results.questions.question"]; 
     [self performSelectorInBackground:@selector(insertDataInQueMaster:) withObject:queDic]; 

     [self getQueCatLevelData:4]; 
    }//and so on for 5 also 

在每個insertDataInQueMaster我正在做以下事情。

  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init]; 
     for(NSDictionary *queDictionary in dictionary) 
     { 

      NSString *strQuery = [NSString stringWithFormat:@"insert into QuestionMaster values(%d,'%@','%@',%d,%d,%d,%d,%d,0,'%@')",[[queDictionary valueForKey:@"questionid"]intValue], [queDictionary valueForKey:@"questiontext"],[queDictionary valueForKey:@"answertext"],[[queDictionary valueForKey:@"categoryid"]intValue],[[queDictionary valueForKey:@"levelid"] intValue],[[queDictionary valueForKey:@"status"]intValue],[[queDictionary valueForKey:@"RangeFrom"]intValue],[[queDictionary valueForKey:@"RangeTo"]intValue],[queDictionary valueForKey:@"Fact"]]; 

      [NSNumber requestWithSynchronousExcuteQuery:strQuery withReturnningError:&error]; 

      if(error) 
      { 
       NSLog(@"error description:%@",[[error userInfo] description]); 
       [[NSUserDefaults standardUserDefaults]setBool:NO forKey:@"FirstTime"]; 
      } 

     } 

     count++; 
     [[NSUserDefaults standardUserDefaults]setInteger:count forKey:@"CheckCount"]; 
     [[NSUserDefaults standardUserDefaults]synchronize]; 
     [pool drain]; 

我做的這些所有的東西,而應用在AppDelegate.m加載,這太背景 的錯誤出現後成功插入數據庫中的一些記錄,所以我認爲這是與數據庫打開和插入沒問題。

我希望我很清楚, 如果有任何困惑請發表評論。 等待回覆。 在此先感謝。

+0

你正在做一些破壞SQLite文件。可以在不鎖定的情況下從兩個不同的線程訪問它(儘管通常SQLite被編譯爲保護自己免受此影響),也可能是您正在執行的其他文件操作(例如意外掉落重新初始化數據庫的路徑)。 –

+0

(請記住,如果您在多個線程中使用SQLite,*必須*以某種方式序列化/鎖定對數據庫的訪問,以防止衝突。) –

回答

2

歡呼,我得到了簡單的解決這個

我剛纔放置

@synchronized(self) 
    { 
     //My Whole Code for Background methods goes here 
    } 

,就是這樣,它解決了我的錯誤和崩潰了。

@synchronized()指令鎖定一段代碼以供單個線程使用。其他線程將被阻塞,直到線程退出受保護的代碼 - 也就是說,當執行繼續超過@synchronized()塊中的最後一條語句時。

@synchronized()指令將任何Objective-C對象(包括self)作爲其唯一參數。

+3

您只需確保在*中使用* same *'self'所有*您執行數據庫操作的位置。還要確保在第一個'select'到'finalize'的同步範圍內覆蓋* entire *操作。 –

+0

非常感謝你 – Anand

+0

非常感謝,對我有用 –