Reputation: 1129
I have More than 5000 Record to be loaded from Server to my App and Inserted into Database. While inserting these records after some successful insertion I am getting this kind of error and my app crashes.
database is malformed in iPhone + error disk i/o error
Following are code snippet that could be useful,Here I am getting Records in 5 Categories One by One and Inserting them into Database
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
in each insertDataInQueMaster I am doing following things.
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];
I am doing These all things while App is Loading in AppDelegate.m , that too in Background The Error Comes After successfully inserting few records in Database so I think there is no problem with Database opening and Inserting.
I hope I am clear, Please comment if any confusion. Waiting for reply. Thanks in advance.
Upvotes: 0
Views: 1614
Reputation: 1129
hurray, I got the Simple Solution to this
I just Placed
@synchronized(self)
{
//My Whole Code for Background methods goes here
}
and that's it, it solved my error and crash too.
The @synchronized()directive locks a section of code for use by a single thread. Other threads are blocked until the thread exits the protected code—that is, when execution continues past the last statement in the @synchronized() block.
The @synchronized() directive takes as its only argument any Objective-C object, including self.
Upvotes: 2