我想使用单例 UIApplication 来访问 AppDelegate 的 managedObjectContext。但是当我写
[[[UIApplication sharedApplication] delegate] managedObjectContext]
或
[[[UIApplication sharedApplication] delegate] __managedObjectContext]
它不起作用。
但是这条线工作正常:
NSLog(@"Seeking for the AppDelegate : %@", [[[UIApplication sharedApplication] delegate] class]);
你有解决办法吗? 尼尔斯
最佳答案
像这样使用单例是不好的做法,甚至在 Core Data 文档中明确反对:
A view controller typically shouldn’t retrieve the context from a global object such as the application delegate. This tends to make the application architecture rigid. Neither should a view controller typically create a context for its own use. This may mean that operations performed using the controller’s context aren’t registered with other contexts, so different view controllers will have different perspectives on the data.
When you create a view controller, you pass it a context. You pass an existing context, or (in a situation where you want the new controller to manage a discrete set of edits) a new context that you create for it. It’s typically the responsibility of the application delegate to create a context to pass to the first view controller that’s displayed.
依赖注入(inject)(即给 View Controller 它需要的东西)几乎在所有情况下都更好。在整个应用程序代码中看到 [[UIApplication sharedApplication] delegate] 确实不好,因为它使代码难以重用,难以为其编写测试等。
关于iphone - 单例 managedObjectContext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6622699/
在Ruby中,方法puts是Kernel的单例方法模块。通常,当一个模块是included或extend由另一个模块编辑,该模块(但不是它的单例类)被添加到继承树中。这有效地使模块的实例方法可用于模块或其单例类(分别用于include和extend)......但混合模块的单例方法仍然无法访问,因为单例类从未将模块添加到继承树中。那么为什么我可以使用puts(和其他内核单例方法)?Kernel.singleton_methods(false)#=>[:caller_locations,:local_variables,:require,:require_relative,:autolo
为什么instance_variables方法不显示针对变量a的@var_one?a=Object.newdefa.my_eval;yieldenda.my_eval{@var_one=1}a.instance_variables#=>[]instance_variables#=>[@var_one] 最佳答案 你应该使用instance_eval:a.instance_eval{@var_one=1}=>1a.instance_variables=>[:@var_one]当你使用普通的eval时,你在当前self的上下文中定义你的
我正在尝试用ruby初始化一个单例。这是一些代码:classMyClassattr_accessor:var_i_want_to_init#singleton@@instance=MyClass.newdefself.instance@@instanceenddefinitialize#tried1.initialize,2.new,3.self.initialize,4.self.newputs"I'mbeinginitialized!"@var_i_want_to_init=2endend问题是从未调用初始化,因此从未初始化单例。我尝试将init方法命名为initialize、
类方法和单例方法是一样的还是不一样的?这是一个例子。classCdefself.classmethodputs"classmethod#{self}"endendC.classmethod#classmethodCc=C.newdefc.singletonmethodputs"instancemethod#{self}"endc.singletonmethod#instancemethod# 最佳答案 Ruby中发生的大多数事情都涉及类和模块,包括实例方法的定义classCdeftalkputs"Hi!"endendc=C.newc
假设我们有这个Ruby类:classMyClassclass然后,让我们实例化MyClass并向对象的元类添加另一个常量:m=MyClass.newclass对象单例常量没有问题:m.singleton_class::OBJ_MC_CONST#=>50true但不完全是我对类单例常量的期望:MyClass.singleton_class::MC_CONST#=>3030false为什么MyClass类的元类上的.constants方法返回的数组不包含:MC_CONST?我在这里缺少什么?谢谢。编辑1:这毕竟是MRI2.x中的一个错误。我已经向Ruby核心团队提交了一个新问题:https
假设一个类需要加载一个外部库,它需要一些时间来加载,因此应该只加载一次。两种自然的解决方案是使用单例模式或单态模式。在Ruby的这个特定上下文中,这两种解决方案有什么优势吗?例如:#UsingaSingletonclassrequire'singleton'classParserincludeSingletondefinitialize@parser=load_external_libraryenddefparse(sentence)@parser.parse(sentence)endend#Thencallingusing...Parser.instance.parse(senten
我正在构建一个与RubyonRails后端对话的iPhone应用程序。RubyonRails应用程序还将为Web用户提供服务。restful_authentication插件是提供快速和可定制的用户身份验证的绝佳方式。但是,我希望iPhone应用程序的用户在新列中存储一个由手机的唯一标识符([[UIDevicedevice]uniqueIdentifier])自动创建的帐户。稍后,当用户准备好创建用户名/密码时,帐户将更新为包含用户名和密码,iPhone唯一标识符保持不变。用户在设置用户名/密码之前不能访问该网站。然而,他们可以使用iPhone应用程序,因为该应用程序可以使用它的标识符
这可能是过早的优化,或过早的过度谨慎,但我避免在一些类上使用单例,因为我担心在未来我需要以多线程方式运行我的应用程序,而且单例会造成冲突和困惑。单例在Ruby中是否有这个问题,或者是否有某种内置的命名空间,以便当一个类引用单例时,只返回同一线程上的单例?编辑:澄清这些是可观察的类,当更新时会导致其他正在观察它们的类更新。我不确定这是否是线程安全的,但我知道现在我正在大量传递这些可观察的类,这有点烦人。而且它们看起来确实像是自然的单例类。 最佳答案 这是一个使单线程安全的例子。您必须像对待任何其他具有状态(@things)并且不是不可
关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion使用Rails中的单例混合,我可以在Rails应用程序的范围内创建一个单例类。但我想知道有没有办法在特定请求的范围内创建它?
我已经定义了一个模块Vehicle这样的moduleVehicleclass调用Vehicle.singleton_methods返回[:build]。如何检查Vehicle定义的所有私有(private)单例方法? 最佳答案 在Ruby1.9+中,您可以简单地执行以下操作:Vehicle.singleton_class.private_instance_methods(false)#=>[:background]在Ruby1.8中,事情有点复杂。Vehicle.private_methods#=>[:background,:inc