首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >-subscribeNext:有效,但RAC()不起作用

-subscribeNext:有效,但RAC()不起作用
EN

Stack Overflow用户
提问于 2014-03-17 18:53:35
回答 2查看 839关注 0票数 0

这项工作如预期的那样:

代码语言:javascript
运行
复制
        // Return a sequence for photos
    [[[[[[RACObserve(self, event.photos) filter:^BOOL(id value) { return value != nil ; }] flattenMap:^RACStream *(NSDictionary *photos)
        {
        NSLog(@"Got photos: %@" , photos) ;
        return photos.rac_sequence.signal ;
        }]

    // Consider each photo
    filter:^BOOL(NSDictionary *photoDescriptor)
        {
        NSLog(@"Descriptor: %@" , photoDescriptor) ;
        return ((NSNumber *)photoDescriptor[@"primary"]).boolValue ;
        }]

    // Load the selected photo
    map:^id(NSDictionary *selectedPhotoDescriptor)
        {
        NSLog(@"Photo URL: %@" , selectedPhotoDescriptor[@"url"]) ;
        return [[AsyncImageFetcher imageAtURL:[NSURL URLWithString:selectedPhotoDescriptor[@"url"]] cache:YES] firstOrDefault:[UIImage imageNamed:@"detail_placeholder"]] ;
        }]

    // Deliver on main thread
    deliverOn:RACScheduler.mainThreadScheduler]

    subscribeNext:^(id x)
        {
        ((UIImageView *)self.headerView).image = x ;
        }] ;

这没有;图像从未设置过:

代码语言:javascript
运行
复制
    RAC( ((UIImageView *)self.headerView), image ) =

    // Return a sequence for photos
    [[[[[RACObserve(self, event.photos) filter:^BOOL(id value) { return value != nil ; }] flattenMap:^RACStream *(NSDictionary *photos)
        {
        NSLog(@"Got photos: %@" , photos) ;
        return photos.rac_sequence.signal ;
        }]

    // Consider each photo
    filter:^BOOL(NSDictionary *photoDescriptor)
        {
        NSLog(@"Descriptor: %@" , photoDescriptor) ;
        return ((NSNumber *)photoDescriptor[@"primary"]).boolValue ;
        }]

    // Load the selected photo
    map:^id(NSDictionary *selectedPhotoDescriptor)
        {
        NSLog(@"Photo URL: %@" , selectedPhotoDescriptor[@"url"]) ;
        return [[AsyncImageFetcher imageAtURL:[NSURL URLWithString:selectedPhotoDescriptor[@"url"]] cache:YES] firstOrDefault:[UIImage imageNamed:@"detail_placeholder"]] ;
        }]

    // Deliver on main thread
    deliverOn:RACScheduler.mainThreadScheduler] ;

为什么?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-03-17 23:05:50

以下是起作用的版本:

代码语言:javascript
运行
复制
    // When there's a new image, fetch it, and set the headerView (which by default is an UIImageView)
RAC( self, imageView.image ) =

    // Return a sequence for photos
    [[[[RACObserve(self, event.photos) ignore:nil] flattenMap:^RACStream *(NSDictionary *photos)
        {
        NSLog(@"Got photos: %@" , photos) ;
        return photos.rac_sequence.signal ;
        }]

    // Consider each photo
    filter:^BOOL(NSDictionary *photoDescriptor)
        {
        NSLog(@"Descriptor: %@" , photoDescriptor) ;
        return ((NSNumber *)photoDescriptor[@"primary"]).boolValue ;
        }]

    // Load the selected photo
    flattenMap:^RACStream *(NSDictionary *selectedPhotoDescriptor)
        {
        NSLog(@"selected photo desc: %@" , selectedPhotoDescriptor) ;
        return [AsyncImageFetcher imageAtURL:[NSURL URLWithString:selectedPhotoDescriptor[@"url"]] cache:YES] ; // This will -deliverOn: the main thread
        }] ;

注意,AsyncImageFetcherimageAtURL:cache:返回一个信号。

有关此解决方案的几个注意事项:

首先,我必须创建一个新的私有属性self.imageView,它只会返回self.headerView。其原因与RAC()宏可以接受的参数有关。我开始怀疑我传递给RAC()的参数是否导致了我的问题,所以我简化了它,在前面提到的私有属性中进行了转换:

代码语言:javascript
运行
复制
@synthesize imageView ;
- (UIImageView *) imageView
{
return (id) self.headerView ;
}

然后我试着:

代码语言:javascript
运行
复制
RAC( self.imageView, image ) = …

但这不管用。然后我试着

代码语言:javascript
运行
复制
RAC( self, imageView.image ) = …

这起作用了!然后我想,让我们去掉self.imageView,看看它是否与宏中的强制转换一起工作:

代码语言:javascript
运行
复制
RAC( self, (((UIImageView *)headerView).image) ) = …

不幸的是,这会产生语法错误。

因此,使用强制转换私有属性的解决方案可以工作,但它感觉像是一个解决方案。我接受这个答案,因为它对我有用,但我仍然想知道是否有一种方法可以做到这一点,而不创建(冗余) imageView属性。

票数 0
EN

Stack Overflow用户

发布于 2015-04-21 02:55:48

我也遇到过类似的问题,以下是我的一些想法。

  1. RAC(self.imageView, image)self.imageView上绑定image密钥路径,而RAC(self, imageView.image)self上绑定imageView.image密钥路径。这基本上意味着信号在第一种情况下只绑定到属性image of self.imageView,而在第二种情况下绑定到selfimageView.image。这种差异在大多数情况下是可以忽略不计的,但根据您所描述的,差异可能是至关重要的。如果绑定发生在创建imageView之前,那么只有RAC(self, imageView.image)才能工作,因为self.imageView还不存在。(这很可能发生在imageView是IBOutlet并且绑定发生在viewDidLoad之前)。
  2. subscribeNext:工作的原因可能是因为当异步信号发送下一个(这需要时间)时,imageView已经创建了,所以您可以成功地将图像分配给它。
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22462905

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档