我应该如何在多个视图控制器中使用 Facebook 对象
过去几天我一直在使用 Facebook iOS API,并遇到了一些问题。如果我有两个不同的视图控制器,两者都需要使用 Facebook 对象,我应该如何共享该对象?有必要分享吗?我一直在考虑做一个单身人士。我想做的是创建一个类“SocialMedia”,它有一个名为“sharedFacebook”的类方法。
然后我有另一个名为 PREFIXFacebook 的类,我在其中遵守会话协议并创建一个对象。该类有一个名为 facebook 的属性。然后我在 SocialMedia 类中实例化此类,并向其发送方法“facebook”,并将返回的对象分配给 SocialMedia 中的静态 Facebook 变量。这是糟糕的设计吗?我之前没有做过太多单例,我什至不知道是否有必要,但我不想重复代码。
另一种方法是在应用程序委托中执行此操作,但我在这里阅读了一篇有关堆栈溢出的文章,并在斯坦福大学的 iOS 视频之一中听说,对全局执行此类操作是不好的。有什么想法吗?
I have been playing with the Facebook iOS API the past days and I got some questions. If I have two distinct view controllers that both need to use the Facebook object how should I share the object? And is it necessary to share it at all? I have been thinking of making a singleton. What I thought of doing was making a class "SocialMedia" that has a class method named "sharedFacebook".
Then I have another class named PREFIXFacebook where I conform to the session protocol and make an object. This class has an attribute named facebook. Then I instantiate this class in the SocialMedia class and send the method "facebook" to it and assign the returned object to the static Facebook variable in SocialMedia. Is that bad design? I have not been doing much singletons before, and I don't even know if it is necessary, but I don't want to duplicate the code.
Another approach would be to do this in the app delegate, but I read a post here on stack overflow and heard in one of the stanford iOS videos that it is bad to do such things with the global. Any thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
与大多数事情一样,全局变量适度的话也不错。如果你滥用它们,你的内存将会不足,但我认为在这种情况下这是合理的。单例本质上是一个全局变量,我相信你的计划是一个很好的计划。它不仅可以帮助您使用 Facebook,还可以帮助您使用其他单点登录网站,例如 Twitter 和 Google+。
为了保持较小的占用空间,您不想在 Singleton 类中存储一百万个东西。尝试仅存储避免重新身份验证所需的最低限度。这可能仅意味着 Facebook 对象本身,而不是它的附加包装类。
As with most things, global variables are not bad in moderation. If you abuse them, you will be running short on Memory, but I think it is justified in this case. A singleton is essentially a global variable and I believe your plan here is a good one. Not only will it help you with Facebook, but it will help with other single-login sites such as Twitter and Google+.
To keep things with a small footprint, you don't want to store a million things in the Singleton class. Try to only store the bare minimum needed to avoid re-authenticating. This probably means only the Facebook object itself, and not an additional wrapper class for it.