2012-10-22 3 views
1

안녕하세요 저는 Riccardo이고 저는 인디 개발자입니다!UI Freeze with Magical Record

나는 objective-c에서 프로그래밍을 시작한 적이별로 없으며, 아직 초보자입니다. : D

코어 데이터를 배우기 시작했고 동시성에 직면하기까지 모든 것이 잘되고있었습니다! 핵심 데이터 사용 내 앱은 저장 작업 중에 차단을 계속했기 때문에 백그라운드 데이터로 코어 데이터를 검색하고 마법 레코드를 발견했습니다.

마법 기록으로 나는 코드의 복잡성을 크게 줄였습니다. :) 하지만 여전히 한 가지 문제가 있습니다. 나는 마법 기록에 관한 모든 것을 읽었지만, 다른 방법을 사용하여 그것을 고치려고했지만 UI가 정지되었습니다. (그래서 그것을 고칠 줄 모르겠다. ...

나열된 4 가지 방법 중 3 가지가 실제로 빠르기 때문에 모든 방법에 문제가 있다고 생각한다. (얼어 붙은 UI보기) 유일한 것은 방법으로 큰 문제가되고 실제로 동결을 알 수 있습니다 ->

  • (void) persistApps FromArray : (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]; 



      } 
     }]; 

    } 

답변

2

이 방법으로 시도 :

[MagicalRecord saveInBackgroundWithBlock^(NSManagedObjectContext * localContext) 블록 {}];

사용할 수있는 여러 가지 방법이 있지만 이것이 좋은 출발점이 될 것입니다. MagicalRecord가 당신을위한 컨텍스트를 만들고 모든 것이 백그라운드 스레드에서 발생합니다. 또한 부모 컨텍스트에 자동으로 저장됩니다.

MagicalRecord 뒤에있는 사람인 Saul Mora의 Core Data and Threads, Without the Headache이 위대한 기사를 읽는 것이 좋습니다. MagicalRecord의 많은 이점에 대해 알려주는 훌륭한 책입니다.

행운을 빈다.

+0

감사합니다. 귀하의 조언을 따르겠습니다. :) –