Note that there are some explanatory texts on larger screens.

plurals
  1. POCore Data freezing main thread
    text
    copied!<p>I am kinda new to Core Data, and have encountered some wierd stuff when making a point counter for a game.</p> <p>I have this code for creating a new Point-object for a match (the static holder "button.pointHolder" is not a core data-object):</p> <pre><code>NSLog(@"before block"); [self.match.managedObjectContext performBlock:^{ NSLog(@"in block"); PointHolderType *holderType = [self.matchController insertPointHolderForTeam:self.match.homeTeam withStaticHolder:button.pointHolder]; dispatch_async(dispatch_get_main_queue(), ^{ NSLog(@"in block dispatch_get_main_queue"); //Updating UI }); }]; NSLog(@"after block"); </code></pre> <p>This seems to block the main thread when my core data is growing. (It doesn't matter if its a new match instance without point holders or not. It is lagging when my core data is growing no matter what)</p> <p>The logs:</p> <pre><code>2013-11-04 11:48:53.059 [463:60b] before block 2013-11-04 11:48:53.060 [463:60b] after block 2013-11-04 11:48:53.062 [463:60b] in block 2013-11-04 11:48:53.606 [463:60b] in block dispatch_get_main_queue </code></pre> <p>And I'm creating my object like this:</p> <pre><code>- (PointHolderType *)insertPointHolderForTeam:(NSString*)team withStaticHolder:(PointHolder *)holder { PointHolderType *holderType = [NSEntityDescription insertNewObjectForEntityForName:@"PointHolderType" inManagedObjectContext:self.match.managedObjectContext]; holderType.type = [NSNumber numberWithInt:holder.pointCounterType]; holderType.team = team; holderType.points = [NSNumber numberWithInteger:holder.pointsAdded]; holderType.match = self.match; return holderType; } </code></pre> <p>Also, if i remove this line:</p> <pre><code>holderType.match = self.match; </code></pre> <p>I get a fully responsive UI. This makes me wonder if it has something to do with my managedObjectContext which I'm using from the "match" itself. </p> <p>Other info:</p> <p>I'm creating my match like this:</p> <pre><code>Match *newMatch = [NSEntityDescription insertNewObjectForEntityForName:@"Match" inManagedObjectContext:self.managedObjectContext]; </code></pre> <p>And my managedObjectContext like this:</p> <pre><code>-(void) useDocument { NSURL *url = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]; url = [url URLByAppendingPathComponent:@"brannboll_document"]; UIManagedDocument *document = [[UIManagedDocument alloc] initWithFileURL:url]; if(![[NSFileManager defaultManager] fileExistsAtPath:url.path]) { //create [document saveToURL:url forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success) { if(success) { self.managedObjectContext = document.managedObjectContext; } }]; } else if(document.documentState == UIDocumentStateClosed) { //Open it [document openWithCompletionHandler:^(BOOL success) { self.managedObjectContext = document.managedObjectContext; }]; } else //try to use it { self.managedObjectContext = document.managedObjectContext; } } </code></pre> <p>Any ideas? What am I missing?</p> <p>EDIT: Thanks to CouchDeveloper in the comments, I managed to find the root of the problems; My parent Core Data Table View controller, whose fetchrequest got constantly called when adding relations to a Match-object. Why? You tell me. I solved it by nil-ing the actual fetchrequest in viewdiddisappear, and vice verca.</p> <p>Thanks!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload