Singleton与第三方库 [英] Singleton vs. third-party libraries

查看:155
本文介绍了Singleton与第三方库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我熟悉的概念Singleton 和这种机制是相当方便,但...

如果我想要一个,共享的一些第三方类的实例会发生什么例如 AFHTTPRequestOperation 或者可能是一些魔法记录?

当我在许多控制器中使用外部类中的一个对象时, >

解决方案

我不熟悉

code> Magical Record 但是对于 AFNewtorking ,在许多情况下做一个单例很有意义。



虽然 AFHTTPRequestOperation 的单例听起来不正确。更好的候选者是 AFHTTPRequestOperationManager



报价从 AFHTTPRequestOperationManager类参考


开发人员针对iOS 7或Mac OS X 10.9或更高版本,广泛地与Web服务交易
鼓励子类
AFHTTPSessionManager,提供一个类方法返回一个共享的
单例对象上的认证和其他配置可以
在应用程序之间共享。



对于面向iOS 6或Mac OS X 10.8或更早版本的开发人员,
AFHTTPRequestOperationManager可用于类似


在<2.0版本中与 AFHTTPCLient / p>

一个很好的参考也是这个教程由Scott Sherwood (一如既往)Ray Wenderlich博客。


I'm familiar with the concept of Singleton and this mechanism is pretty handy but..
What happens in case I want one, shared instance of some third-party class e.g. AFHTTPRequestOperation or maybe some Magical Record?
What should I do when I'm using one object from external class in many controllers? Or maybe it is a good practice to instantiate a new object in each controller?

解决方案

I'm not familiar with Magical Record but for AFNewtorking it definitely makes sense to make a singleton in many cases.

Though singleton of AFHTTPRequestOperation doesn't sound quite right. Much better candidate would be AFHTTPRequestOperationManager.

Quote from AFHTTPRequestOperationManager Class Reference

Developers targeting iOS 7 or Mac OS X 10.9 or later that deal extensively with a web service are encouraged to subclass AFHTTPSessionManager, providing a class method that returns a shared singleton object on which authentication and other configuration can be shared across the application.

For developers targeting iOS 6 or Mac OS X 10.8 or earlier, AFHTTPRequestOperationManager may be used to similar effect.

It was similar with AFHTTPCLient in <2.0 versions.

One good reference would also be this tutorial by Scott Sherwood on (as always) Ray Wenderlich blog.

这篇关于Singleton与第三方库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆