UI冻结与魔法记录

时间:2012-10-22 00:22:10

标签: core-data freeze magicalrecord background-thread

您好我是Riccardo,我是独立开发者!

我在Objective-c中开始编程并不是很多,我仍然是新手! :d

我开始学习核心数据,一切都很顺利,直到我遇到并发! 使用Core Data,我的应用程序在保存操作期间继续阻止,因此我使用背景上下文搜索核心数据,然后找到了Magical Record。

使用Magical Record我大大降低了代码的复杂性:) 但我还有一个问题,我读了关于魔法记录的所有内容,我试图用不同的方法修复它但是UI冻结仍然存在:(所以我不知道如何解决它...

虽然我认为所有方法都存在问题(查看冻结用户界面),但列出的4种方法中有3种速度非常快,因此无法察觉。 作为一个巨大的问题而你实际上可以注意到冻结的唯一方法就是这个 - >

  • (void)persistAppsFromArray:(NSArray *)arrayOfApps inResearch:(NSDictionary *)info;

我拿一个数组来创建所有的应用程序对象,然后我将这些应用程序关联(在获取研究之后,我使用字典来创建谓词)。

这是我的代码。它实际上是一个管理核心数据的整个类。 我不会在调用这些方法的类中使用队列或线程,所以我认为这里有一些问题需要解决: - /

我提前感谢你:)。

#import "CoreDataHelper.h"
#import "CoreData+MagicalRecord.h"


@implementation CoreDataHelper

#pragma mark - Research creator helper

+ (ResearchType *) fetchResearchWithDictionary: (NSDictionary *) info {

// Fetch
// predicates for fetch
NSPredicate * predicateCategory = [NSPredicate predicateWithFormat:@"researchCategory = %@" , [info objectForKey: @"Category"]];
NSPredicate * predicateType = [NSPredicate predicateWithFormat:@"researchType = %@" , [info objectForKey: @"Type"]];
 NSPredicate * predicatePrefix = [NSPredicate predicateWithFormat:@"researchPrefix = %@" , [info objectForKey: @"Prefix"]];

 NSArray * predicates = [NSArray arrayWithObjects: predicateCategory, predicateType, predicatePrefix, nil];

        // create the composite predicate
        NSPredicate * predicateComposito = [NSCompoundPredicate andPredicateWithSubpredicates:predicates];

    // obtain the result
    // Passo la ricerca al brain
   return [ResearchType MR_findFirstWithPredicate: predicateComposito];
}

   + (void)updateResearchWithDictionary:(NSDictionary *) info withCompletionBlock:(completion_block)updatedResearch {

    // Get the context
    NSManagedObjectContext *localContext    = [NSManagedObjectContext MR_context];

    // predicates
    NSPredicate * predicateCategory = [NSPredicate predicateWithFormat:@"researchCategory = %@" , [info objectForKey: @"Category"]];
    NSPredicate * predicateType = [NSPredicate predicateWithFormat:@"researchType = %@" , [info objectForKey: @"Type"]];
    NSPredicate * predicatePrefix = [NSPredicate predicateWithFormat:@"researchPrefix = %@" , [info objectForKey: @"Prefix"]];

    NSArray * predicates = [NSArray arrayWithObjects: predicateCategory, predicateType, predicatePrefix, nil];

    // composite predicate
    NSPredicate * predicateComposito = [NSCompoundPredicate andPredicateWithSubpredicates:predicates];

// create research object 
    ResearchType *research = [ResearchType MR_findFirstWithPredicate: predicateComposito inContext:localContext];



// new properties
    research.researchDate      = [info objectForKey:@"Research Date"];
    research.researchLimit     = [info objectForKey:@"Limit"];
    //research.apps              =  nil;

  [localContext MR_saveInBackgroundCompletion:^{

      updatedResearch(research);

  }];
 }

   + (void) persistResearchWithDictionary: (NSDictionary *) info withCompletionBlock:(completion_block)savedResearch {

// Get the context
NSManagedObjectContext *localContext    = [NSManagedObjectContext MR_context];

// Create a new Person in the current thread context
ResearchType * research   = [ResearchType MR_createInContext:localContext];

// Set properties
research.researchCategory    = [info objectForKey:   @"Category"];
research.researchPrefix      = [info objectForKey:   @"Prefix"];
research.researchCategoryUrl = [info objectForKey:   @"Category Code Url"];
research.researchType        = [info objectForKey:   @"Type"];


research.researchCountry     = [info objectForKey:   @"Country"];
research.researchCountryCode = [info objectForKey:   @"Country Code"];
research.researchDate        = [info objectForKey:   @"Research Date"];
research.researchDevice      = [info objectForKey:   @"Device"];
research.researchLimit       = [info objectForKey:   @"Limit"];

// Save changes
 [localContext MR_saveInBackgroundCompletion:^{

   savedResearch(research);
 }];

 }

    + (void) persistAppsFromArray: (NSArray *) arrayOfApps inResearch: (NSDictionary *) info  {


// Get the local context
    NSManagedObjectContext *localContext    = [NSManagedObjectContext MR_context];

 [localContext MR_saveInBackgroundCompletion:^{


     // predicates
     NSPredicate * predicateCategory = [NSPredicate predicateWithFormat:@"researchCategory = %@" , [info objectForKey: @"Category"]];
     NSPredicate * predicateType = [NSPredicate predicateWithFormat:@"researchType = %@" , [info objectForKey: @"Type"]];
     NSPredicate * predicatePrefix = [NSPredicate predicateWithFormat:@"researchPrefix = %@" , [info objectForKey: @"Prefix"]];

     NSArray * predicates = [NSArray arrayWithObjects: predicateCategory, predicateType, predicatePrefix, nil];

     // Composite predicate
     NSPredicate * predicateComposito = [NSCompoundPredicate andPredicateWithSubpredicates:predicates];

     ResearchType * researchBgContext = [ResearchType MR_findFirstWithPredicate: predicateComposito];

     for (NSDictionary * info in arrayOfApps) {

         AppInDb * app             = [AppInDb MR_createEntity];

         app.appName                  = [info objectForKey: @"App Name"];
         app.appItunesLink            = [info objectForKey: @"iTunes Link"];
         app.appBundleID              = [info objectForKey: @"Bundle ID"];
         app.appID                    = [info objectForKey: @"ID"];
         app.appPriceLabel            = [info objectForKey: @"Price Label"];
         app.appReleaseDate           = [info objectForKey: @"Release Date String"];
         app.appIconImageLink         = [info objectForKey: @"Image Link 100"];
         app.appDeveloperPageLink     = [info objectForKey: @"Developer Page Link"];
         app.appDeveloper             = [info objectForKey: @"Developer"];
         app.appRights                = [info objectForKey: @"Rights"];
         app.appDescription           = [info objectForKey: @"App Description"];
         app.appCategory              = [info objectForKey: @"App Category"];
         app.appCategoryNumber        = [info objectForKey: @"Category Number For Url"];


         // adding app to the related research (one to many relationship)
         [researchBgContext addAppsObject:app];



           }
       }];

     }

1 个答案:

答案 0 :(得分:2)

尝试使用此方法:

[MagicalRecord saveInBackgroundWithBlock:^(NSManagedObjectContext * localContext)block { }];

您可以使用许多其他方法,但这应该会给您一个良好的开端。 MagicalRecord将为您创建上下文,一切都在后台线程上进行。它还将自动保存到父上下文。

我建议阅读这篇伟大的文章:Core Data and Threads, Without the Headache由Saul Mora担任,他是MagicalRecord背后的人。这是一个很好的阅读,告诉你MagicalRecord的许多优点。

祝你好运!