0

我有超过 5000 条记录要从服务器加载到我的应用程序并插入数据库。在成功插入后插入这些记录时,我遇到了这种错误并且我的应用程序崩溃了。

数据库在 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 中加载 App 时正在做这些所有事情,这也是在后台成功在数据库中插入几条记录后出现错误,所以我认为数据库打开和插入没有问题。

我希望我清楚,如果有任何困惑,请发表评论。等待回复。提前致谢。

4

1 回答 1

2

欢呼,我得到了简单的解决方案

我刚刚放置

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

就是这样,它也解决了我的错误和崩溃。

@synchronized() 指令锁定一段代码以供单个线程使用。其他线程被阻塞,直到线程退出受保护的代码——也就是说,当执行继续超过 @synchronized() 块中的最后一条语句时。

@synchronized() 指令将任何 Objective-C 对象(包括 self)作为其唯一参数。

于 2012-10-18T12:13:47.783 回答