访问器/获取器和延迟初始化

发布于 2024-09-18 21:02:05 字数 739 浏览 4 评论 0原文

我有一个关于覆盖自动生成的访问器方法的问题。以下行不通(我相信),因为每个 getter 都引用另一个 getter。是否存在访问器方法不应使用其他访问器方法的规则,或者您是否只需要单独注意这些情况?

-(UIImage *) image{

    if(image == nil){
        if(self.data == nil){
            [self performSelectorInBackground: @selector(loadImage) withObject: nil]
        }else{
            self.image = [UIImage imageWithData: self.data];
        }
    }

    return image;
}

-(NSData *) data {

    if(data == nil){
        if(self.image == nil){
            [self performSelectorInBackground: @selector(loadData) withObject: nil]
        }else{
            self.data = UIImageJPEGRepresentation(self.image, 0.85);
        }
    }

    return data;
}

我必须强调,这里提供的图像使用只是一个示例,关于在这个特定示例中做什么的想法不如一般情况那么重要。

I have a question about overriding auto-generated accessor methods. The following would not work (I believe) because each getter references the other getter. Is there a rule that accessor methods should not use other accessor methods, or do you just have to watch out for these situations individually?

-(UIImage *) image{

    if(image == nil){
        if(self.data == nil){
            [self performSelectorInBackground: @selector(loadImage) withObject: nil]
        }else{
            self.image = [UIImage imageWithData: self.data];
        }
    }

    return image;
}

-(NSData *) data {

    if(data == nil){
        if(self.image == nil){
            [self performSelectorInBackground: @selector(loadData) withObject: nil]
        }else{
            self.data = UIImageJPEGRepresentation(self.image, 0.85);
        }
    }

    return data;
}

I have to emphasize that the image use presented here is an example, and thoughts concerning what to do in this particular example are less important than in the general case.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

汹涌人海 2024-09-25 21:02:05

首先,不要为了自己的利益而太聪明。如果您想克服某些瓶颈,请首先测量并确保它确实存在。我相信 UIImageNSData 都会进行一些内部延迟加载,因此您的代码可能基本上毫无用处。其次,即使您真的想手动执行类似的操作,也请尝试将缓存代码拆分到一个单独的类中,这样就不会污染主类的代码。

没有关于访问器的规则(至少据我所知没有),因为人们不会在访问器中进行太多的延迟加载。有时我会陷入由懒惰的 [UIViewController loadView][UIViewController view] 组合引起的无限循环,但仅此而已。

First, don’t be too smart for your own good. If you want to get over some bottleneck, first measure and make sure it’s really there. I believe that both UIImage and NSData do some internal lazy loading, so that your code might be essentially useless. Second, even if you really wanted to do something like that by hand, try splitting the caching code into a separate class, so that you don’t pollute the code of the main class.

There is no rule about accessors (at least no that I know of), because people don’t do much lazy loading in accessors. Sometimes I get myself caught by an endless loop caused by lazy [UIViewController loadView] in combination with [UIViewController view], but that’s about it.

软糯酥胸 2024-09-25 21:02:05

没有什么可以禁止它,但你肯定会编写一些令人困惑的代码。本质上,这两个属性具有循环依赖关系。这很难阅读和调试。目前尚不清楚为什么您要在“加载图像”之前“加载数据”,或者为什么您还想在“加载数据”之前支持“加载图像”,或者这两个属性实际上如何确实是两个不同的东西。

There's nothing that forbids it, but you're definitely writing some confusing code. Essentially, these two properties have a circular dependency. This is hard to both read and debug. It's not clear why you'd want to "load the data" before you "load the image", or why you'd also want to support "loading the image" before you "load the data", or really how the two properties are really two different things.

能否归途做我良人 2024-09-25 21:02:05

您在本示例中实际执行的操作可能需要很长时间才能加载;最好保证它是线程安全的。

此外,如果您使数据对象成为一个真正的数据提供者(通常使用协议),与类分开,并使图像容器暴露它正在处理延迟加载的对象(这可能会引起某些人的强烈抗议),那就更好了人们)。

具体来说,您可能想从提供者调用加载数据/图像,从 awakeFromNib 调用它(例如) - 然后加载器运行并在辅助线程上加载数据(特别是如果已下载)。给数据提供者一个回调,通知视图图像已准备好(通常使用协议)。一旦视图获取未归档的图像,数据提供者就会失效。

最后,如果您正在处理应用程序资源,“系统”将为您缓存其中的一些资源,因此您将尝试针对幕后已经优化的内容进行工作。

简而言之,通常是可以的(例如,不是延迟初始化) - 但这种特定设计(正如另一位海报所说)具有应该最小化的循环依赖性。

what you're actually doing in this example could take a long time to load; it is best to ensure that it is thread safe.

furthermore, it would be even better if you made the data object a real data provider (typically using protocols), separate from the class, and to make the image container expose that it is handling a lazily loaded object (which may cause outcry from some people).

specifically, you may want to invoke a load data/image from the provider, call it from awakeFromNib (for example) - then the loader runs off and loads the data on a secondary thread (esp. if downloaded). give the data provider a callback to inform the view that the image is ready (typically using protocols). once the view takes the unarchived image, invalidate the data provider.

finally, if you're dealing with app resources, the 'system' will cache some of this for you sooo you'd be attempting to work against what's already optimised behind the scenes.

in short, it's usually ok (e.g. not lazy initialization) - but this specific design (as another poster said) has a circular dependency which should be minimized.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文