- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我看过一个基于角色的iOS项目,作者在应用程序启动时就启动了几乎所有的 View 和 Controller 。并且主要使用NSNotification来进行它们之间的通信。甚至 NSNotification 也是同一种类型,这意味着所有通知都具有相同的名称:
[[NSNotificationCenter defaultCenter] addObserver:aObserver selector:aSelector name:*notification_name* object:nil];
[[NSNotificationCenter defaultCenter] postNotificationName:*notification_name* object:parameter];
它根据不同的notification.object告诉不同类型的通知,它是NSObject的自定义子类,它只包含一些整数,一些字符串和一些对象,如
@interface Parameter : NSObject
{
// which is an enumeration type to actually define different notification type
ParameterID m_iVCD_ID;
int m_iInt0;
int m_iInt1;
int m_iInt2;
float m_fFloat0;
float m_fFloat1;
float m_fFloat2;
NSString *m_sString0;
NSString *m_sString1;
NSString *m_sString2;
NSMutableArray *m_oArray;
NSObject *m_oObject;
NSObject *m_oObject0;
NSObject *m_oObject1;
NSObject *m_oObject2;
}
我觉得这不是一个很好的主意,因为没有对 notification.object 进行类型检查。基于通知的架构是一个广播系统,因为它对所有通知使用相同的名称。此外,在开始时初始化所有 UIView 和 UIViewController 会消耗大量内存。但是,我没有从控制台看到任何内存警告,而是在使用管理器时在 console.app 中看到。
谁能给一些其他的建议?此架构还有其他不好的方面吗?
最佳答案
我认为这是一个非常非常糟糕的方法。
您评论中的每个论点都是正确的:
I feel that it is not a very good idea because there is no type checking for the notification.object.
当然。
And the notification based architecture is a broadcast system because it uses the same name for all notifications.
除非有意,否则这完全没有意义。
In addition, initializing all UIViews and UIViewControllers at the start cost a lot of memory. However, I haven't seen any memory warning from the console but in console.app when using organizer.
是的,即使没有触发内存警告,它也在浪费内存。 UIView
和 UIViewController
有自己的生命周期,SDK 为您提供了在需要时加载和卸载(分配和释放)资源的方法。
除此之外, Controller 之间的通信应通过 @property
、协议(protocol)或 SDK 提供的任何其他方法来完成。
来自 Apple 文档:
An observer of a given notification may be in a suspended state and not processing notifications immediately.
因此,您不能依赖它们来执行比方说关键任务。
关于ios - iOS项目初始化项目中需要的所有UIViewController和UIView是不是一个好习惯?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23425822/
在我生活的世界中,构建一个将所有飞镖都扔到墙上的解决方案,并希望其中一些能命中靶心是一种非常糟糕的解决方案方法。 那么,我的问题出现了,什么时候在约定可接受的生产系统中使用 INSERT IGNORE
在数据处理时,经常会因为index报错而发愁。不要紧,本次来和大家聊聊pandas中处理索引的几种常用方法。 1.读取时指定索引列 很多情况下,我们的数据源是 CSV 文件。假设
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 6 年前。 Improve
我是一名优秀的程序员,十分优秀!