0

サーバーからアプリにロードしてデータベースに挿入する5000を超えるレコードがあります。挿入が成功した後にこれらのレコードを挿入しているときに、この種のエラーが発生し、アプリがクラッシュします。

iPhoneでデータベースの形式が正しくない+エラーディスクi/oエラー

以下は、役立つ可能性のあるコードスニペットです。ここでは、5つのカテゴリのレコードを1つずつ取得し、データベースに挿入しています。

    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にロードされている間にこれらすべてのことを行っていますが、これもバックグラウンドでエラーが発生しますデータベースにいくつかのレコードを正常に挿入した後、データベースを開いて挿入することに問題はないと思います。

はっきりしているといいのですが、混乱があればコメントしてください。返事を待っています。前もって感謝します。

4

1 に答える 1

2

やあ、私はこれに対する簡単な解決策を手に入れました

配置したばかり

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

それだけです、それは私のエラーを解決し、クラッシュもしました。

@synchronized()ディレクティブは、単一のスレッドで使用するためにコードのセクションをロックします。他のスレッドは、スレッドが保護されたコードを終了するまで、つまり実行が@synchronized()ブロックの最後のステートメントを超えて続行されるまでブロックされます。

@synchronized()ディレクティブは、selfを含むObjective-Cオブジェクトを唯一の引数として取ります。

于 2012-10-18T12:13:47.783 に答える