How about replacing threadDictionary thing with dispatch_queue_get/set_specific? #274

gardenrabbit opened this Issue Oct 10, 2012 · 3 comments


None yet

3 participants


I tried to use [MagicalRecord saveInBackgroundUsingCurrentContextWithBlock:....] in my gcd queue, and I realized that the managed object context had been stored to threadDictionary. However, gcd queue doesn't have a specific thread, and it doesn't have a unique context.

@gardenrabbit gardenrabbit reopened this Oct 10, 2012
Magical Panda Software member

thx for reply. I just tried to write some code about the issue. I wonder if it would be correct.

  • (NSManagedObjectContext *)MR_contextForGCDQueue:(dispatch_queue_t)q
    static const NSString *kMRMOCKey = @"moc";

    CFDictionaryRef dictCF = (CFDictionaryRef)dispatch_queue_get_specific(q, (__bridge CFStringRef)kMRMOCKey);
    NSManagedObjectContext *qContext = [(__bridge NSDictionary *)dictCF objectForKey:@"obj"];

    if (qContext == nil)
    qContext = [self MR_contextWithParent:[NSManagedObjectContext MR_defaultContext]];
    NSDictionary *dic = [NSDictionary dictionaryWithObject:qContext forKey:@"obj"];

    CFDictionaryRef dictCF = CFDictionaryCreateCopy(NULL, (__bridge CFDictionaryRef)dic);
    dispatch_queue_set_specific(q, (__bridge CFStringRef)kMRMOCKey, (void *)dictCF, (dispatch_function_t)CFRelease);

    return qContext;


Storing contexts per thread in the thread’s dictionary is being deprecated in MagicalRecord 3.0. Thanks for the suggestion!

@tonyarnold tonyarnold closed this Dec 29, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment