对于普通实例,我从我的表Folders
中获取数据,如此
+ (void)getCurrentFolderFileForParentFolderID:(NSString *)parentFolderID sortBy:(NSString *)sortBy asending:(BOOL)asending withCompletionHandler:(void(^)(id response,NSError *error))completionHandler {
NSPredicate *filter = [NSPredicate predicateWithFormat:@"parentfolderidlocal = %@", parentFolderID];
NSArray *folders = [Folders MR_findAllSortedBy:sortBy ascending:asending withPredicate:filter];
NSArray *files = [Files MR_findAllSortedBy:sortBy ascending:asending withPredicate:filter];
completionHandler(@{@"folders":folders,@"files":files},nil);
}
它正在工作超精细没有任何问题,但每当我尝试从后台获取数据时我遇到一些奇怪的问题,我尝试了以下方法来获取后台队列中的数据始终获得相同的问题
流程一: 在调度队列中调用它
dispatch_async(fileSystemBackGroundQueue(), ^{
[DataBaseManager getCurrentFolderFileForParentFolderID:parentFolderID sortBy:sortBy asending:asending withCompletionHandler:^(id response, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^(void){
completionHandler(response, error);
});
}];
});
其中fileSystemBackGroundQueue()
如下
dispatch_queue_t fileSystemBackGroundQueue() {
static dispatch_once_t queueCreationGuard;
static dispatch_queue_t queue;
dispatch_once(&queueCreationGuard, ^{
queue = dispatch_queue_create("com.myappname.filesystem.backgroundQueue", DISPATCH_QUEUE_SERIAL);
});
return queue;
}
第二个过程:使用本地上下文进行神奇记录
+ (void)getCurrentFolderFileForParentFolderID:(NSString *)parentFolderID sortBy:(NSString *)sortBy asending:(BOOL)asending withCompletionHandler:(void(^)(id response,NSError *error))completionHandler {
NSManagedObjectContext *localContext = [NSManagedObjectContext MR_context];
NSPredicate *filter = [NSPredicate predicateWithFormat:@"parentfolderidlocal = %@", parentFolderID];
NSArray *folders = [Folders MR_findAllSortedBy:sortBy ascending:asending withPredicate:filter inContext:localContext];
NSArray *files = [Files MR_findAllSortedBy:sortBy ascending:asending withPredicate:filter inContext:localContext];
completionHandler(@{@"folders":folders,@"files":files},nil);
}
或
+ (void)getCurrentFolderFileForParentFolderID:(NSString *)parentFolderID sortBy:(NSString *)sortBy asending:(BOOL)asending withCompletionHandler:(void(^)(id response,NSError *error))completionHandler {
NSManagedObjectContext *localContext = [NSManagedObjectContext MR_context];
[localContext performBlock:^{
NSPredicate *filter = [NSPredicate predicateWithFormat:@"parentfolderidlocal = %@", parentFolderID];
NSArray *folders = [Folders MR_findAllSortedBy:sortBy ascending:asending withPredicate:filter inContext:localContext];
NSArray *files = [Files MR_findAllSortedBy:sortBy ascending:asending withPredicate:filter inContext:localContext];
dispatch_async(dispatch_get_main_queue(), ^(void){
completionHandler(@{@"folders":folders,@"files":files},nil);
});
}];
}
并在上面的调度队列中调用它或使用out dispatch queue
在背景线程的所有三种情况下,在表视图单元格中使用索引路径行的数据时,我遇到以下问题:
但是对于后台线程我得到了对象但没有值(有时候我会得到价值)
请任何人帮忙。我被困在这里。我需要在这里使用调度队列。我对背景线程/调度没有太多了解。如果我在这里做错了什么,请告诉我。
答案 0 :(得分:0)
这个问题不是关于MagicalRecord,而是关于CoreData
两个提示:
1.在从另一个线程获取之前保存到持久存储;
2.托管对象只能在您获取它的线程上使用。