gpt4 book ai didi

ios - NSFetchedResultsController UITableView 的多个实体

转载 作者:可可西里 更新时间:2023-11-01 05:55:57 26 4
gpt4 key购买 nike

我有两个实体,一个叫 Post,一个叫 User。 Post<<---->User是核心数据中的关系。我正在使用 NSFetchedResultsController 来获取我的核心数据堆栈中的所有 Post 记录,然后将它们显示在 UITableView 中。每个单元格都有一个图像,该图像对应于一个 User.profilePicture。

初始化后,我不从服务器下载个人资料图片,我只在它滚动到该单元格时才下载(延迟加载)。下载后,我将下载的图像保存到核心数据堆栈中相应的 User.profilePicture。

有没有办法在我更新用户实体时调用 controllerDidChangeContent?我目前的理解是,我的 NSFetchedResultsController 只能跟随 Post 实体,因为这是我最初设置它要做的,不能遍历和监视关系中的更新,是这样吗?

最佳答案

遗憾的是,我只知道这个问题的一个丑陋的解决方案。

在您的 User .m 文件中实现 setProfilePicture: 如下:

//NOT TESTED IN A MULTITHREADED ENV
- (void) setProfilePicture:(NSData *)data
{
[self willChangeValueForKey:@"profilePicture"];
[self setPrimitiveValue:data forKey:@"profilePicture"];
[self.posts enumerateObjectsUsingBlock:^(Post* p, BOOL *stop) {
[p willChangeValueForKey:@"user"];
[p didChangeValueForKey:@"user"];
}];
[self didChangeValueForKey:@"profilePicture"];
}

这将通知 FRC Post 元素发生了变化。

您可能会找到更多信息 here

编辑:

要获取访问数据,您可以将其添加到您的 User.m:

//UNTESTED
+ (void) mergeToMain:(NSNotification*)notification
{
AppDelegate* appDel = (AppDelegate*)[[UIApplication sharedApplication] delegate];
[appDel.managedObjectContext performSelectorOnMainThread:@selector(mergeChangesFromContextDidSaveNotification:)
withObject:notification
waitUntilDone:YES];
}

- (NSData*)_profilePicture
{
return [self primitiveValueForKey:@"profilePicture"];
}

- (NSData*) profilePicture
{
[self willAccessValueForKey:@"profilePicture"];
NSData* picData = [self primitiveValueForKey:@"profilePicture"];
if (!name) {
__block NSManagedObjectID* objectID = self.objectID;
//This solves the multiple downloads per item by using a single queue
//for all profile pictures download.
//There are more concurrent ways to accomplish that
dispatch_async(downloadSerialQueue, ^{ //define some serial queue for assuring you down download multiple times the same object
NSError* error = nil;
AppDelegate* appDel = (AppDelegate*)[[UIApplication sharedApplication] delegate];
NSManagedObjectContext* context = [[NSManagedObjectContext alloc] init];
[context setPersistentStoreCoordinator:appDel.persistentStoreCoordinator];
[context setUndoManager:nil];
User* user = (User*)[context existingObjectWithID:objectID error:&error];
if (user && [user _profilePicture] == nil) {
NSData *data = //[method to retrieve data from server];
if (data) {
if (user) {
user.profilePicture = data;
} else {
NSLog(@"ERROR:: error fetching user: %@",error);
return;
}
[[NSNotificationCenter defaultCenter] addObserver:[self class] selector:@selector(mergeToMain:) name:NSManagedObjectContextDidSaveNotification object:context];
[context save:&error];
[[NSNotificationCenter defaultCenter] removeObserver:[self class] name:NSManagedObjectContextDidSaveNotification object:context];
}
}
});
}
[self didAccessValueForKey:@"profilePicture"];
return picData;
}

关于ios - NSFetchedResultsController UITableView 的多个实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16242304/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com