您好,我是 Riccardo,我是一名独立开发者!
我开始用objective-c编程的次数并不多,而且我还是个新手!:D
我开始学习 Core Data,一切都很顺利,直到我遇到并发!使用 Core Data,我的应用程序在保存操作期间继续阻塞,所以我用谷歌搜索了带有背景上下文的核心数据,我找到了 Magical Record。
使用 Magical Record,我大大降低了代码的复杂性 :) 但是我还有一个问题,我阅读了有关 Magical Record 的所有内容,我尝试使用不同的方法修复它,但 UI 冻结仍然存在 :( 所以我没有知道如何解决它...
尽管我认为所有方法都存在问题(查看冻结 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];
}
}];
}