Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 可可装订。不更新UI中的属性_Objective C_Macos_Cocoa_Cocoa Bindings - Fatal编程技术网

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是
    NSWindowController
    子类,connectionmanager跟踪登录的用户),然后在其上设置转换器
  • 一个
    NSTableView
    NSArray控制器
    绑定到包含类
    News
    对象的
    NSArray
    。每个属性都有一个属性
    creator
    ,它是
    User
    对象。。因此表中行内的imageview绑定到
    objectValue.creator
    ,然后绑定到同一个转换器
关于为什么这不起作用的一些想法

编辑:我删除了关于“以编程方式”工作的声明。 我看到它在工作,但可能是因为其他原因。 所以…当我设置
user.picture
值时,不会向我的观察对象发送KVO通知… 我开始认为我没有正确处理KVO协议,但我阅读了指南,仍然无法找出错误

对于第一个“case”(
NSImageView
binding),我绑定的用户对象位于包装器内。因此,如果我理解正确,那么包装器对于用户对象应该是兼容KVO的。但我认为:

@property (nonatomic, strong, readonly) User *user;
当然,
User
对象是一个
NSManagedObject
子类(但是我的控制器与
NSMangedObjectContext
没有关系。但我认为这不是问题所在