Objective c 可可装订。不更新UI中的属性
我有一个带有Objective c 可可装订。不更新UI中的属性,objective-c,macos,cocoa,cocoa-bindings,Objective C,Macos,Cocoa,Cocoa Bindings,我有一个带有用户类的模型。用户具有图片NSImage属性(在coredata中设置为可转换)。我想做的是在第一次访问图片时从服务器检索它 第一个解决方案(虽然有效,但我不喜欢)是添加一个类别和另一个NSImage属性。UI绑定到第二个属性。第一次访问它时,它会连接到服务器并检索图像。 这个解决方案的问题是,我必须将对httpclient的引用传递给这个类(我使用AFNetworking,所以AFHTTPClient),我在appdelegate中创建并初始化它。目前它是一个单例,但我不喜欢这个解
用户
类的模型。用户具有图片
NSImage
属性(在coredata中设置为可转换)。我想做的是在第一次访问图片时从服务器检索它
第一个解决方案(虽然有效,但我不喜欢)是添加一个类别和另一个NSImage属性。UI绑定到第二个属性。第一次访问它时,它会连接到服务器并检索图像。
这个解决方案的问题是,我必须将对httpclient的引用传递给这个类(我使用AFNetworking,所以AFHTTPClient
),我在appdelegate中创建并初始化它。目前它是一个单例,但我不喜欢这个解决方案
所以…这是我尝试的第二件事:
我在我的AFHTTPClient
wrapper类中创建了以下方法
- (void)loadPictureForUser:(User*)user
imageLoadedCallback:(void(^)())callback
{
//if picture is already loaded do nothing
if (user.picture) return;
//if the request is already "ongoing" then do nothing
if ([self.imageLoadingRequests objectForKey:user.id]) return;
//We have to make a request
//set the placeholder while we wait for the image
#if TARGET_OS_IPHONE
user.picture = [UIImage imageNamed:@"UnknownUserPicture"];
#else
user.picture = [NSImage imageNamed:@"UnknownUserPicture"];
#endif
//make the request
NSDictionary *params = @{
@"id" : user.id,
@"size" : @(80),
@"count" : @(random() % 1000000)};
AFHTTPClient *httpClient = self.httpClient;
NSURLRequest *imageRequest = [httpClient requestWithMethod:@"GET"
path:ServerGetUserPictureURL
parameters:params];
__weak ConnectionManager *weakSelf = self;
AFImageRequestOperation *imageRequestOperation =
#if TARGET_OS_IPHONE
[AFImageRequestOperation imageRequestOperationWithRequest:imageRequest
imageProcessingBlock:nil
success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
user.picture = image;
//in every case I have to remove the entry from the dictionary
[weakSelf.imageLoadingRequests removeObjectForKey:user.id];
BLOCK_SAFE_EXEC(callback);
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
//in every case I have to remove the entry from the dictionary
[weakSelf.imageLoadingRequests removeObjectForKey:user.id];
}];
#else
[AFImageRequestOperation imageRequestOperationWithRequest:imageRequest
imageProcessingBlock:nil
success:^(NSURLRequest *request, NSHTTPURLResponse *response, NSImage *image) {
//save the picture in the user
user.picture = image;
user.picture.name = user.nickname;
//in every case I have to remove the entry from the dictionary
[weakSelf.imageLoadingRequests removeObjectForKey:user.id];
BLOCK_SAFE_EXEC(callback);
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
//in every case I have to remove the entry from the dictionary
[weakSelf.imageLoadingRequests removeObjectForKey:user.id];
}];
#endif
[self.imageLoadingRequests setObject:imageRequestOperation forKey:user.id];
[httpClient enqueueHTTPRequestOperation:imageRequestOperation];
}
此方法设置占位符,直到检索到正确的图片。
此方法是从自定义的NSValueTransformer
- (id)transformedValue:(id)value
{
if (![value isKindOfClass:[User class]])
return nil;
User *user = value;
[self.connectionManager loadPictureForUser:value imageLoadedCallback:NULL];
return user.picture;
}
占位符设置正确(显然)。
问题是,当设置了正确的图片时,UI不会更新。
我想这可能是我如何绑定属性的问题
我在两个不同的地方使用它:
- 一个简单的
。。我将NSImageView
属性直接绑定到fileowner.connectionManger.user(fileowner是值
子类,connectionmanager跟踪登录的用户),然后在其上设置转换器NSWindowController
- 一个
。NSTableView
绑定到包含类NSArray控制器
对象的News
。每个属性都有一个属性NSArray
,它是creator
对象。。因此表中行内的imageview绑定到User
,然后绑定到同一个转换器objectValue.creator
user.picture
值时,不会向我的观察对象发送KVO通知…
我开始认为我没有正确处理KVO协议,但我阅读了指南,仍然无法找出错误
对于第一个“case”(NSImageView
binding),我绑定的用户对象位于包装器内。因此,如果我理解正确,那么包装器对于用户对象应该是兼容KVO的。但我认为:
@property (nonatomic, strong, readonly) User *user;
当然,User
对象是一个NSManagedObject
子类(但是我的控制器与NSMangedObjectContext
没有关系。但我认为这不是问题所在