Is it a good practice for iOS project to initialize all the UIViewController and UIView needed in a project?

StackOverflow https://stackoverflow.com/questions/23425822

  •  14-07-2023
  •  | 
  •  

Question

I have seen a role-based iOS project in which the author initiates almost all the views and controllers when the application started. And it mainly uses NSNotification for communication between them. Even the NSNotification is of the same kind which means that all the notifications have the same name:

[[NSNotificationCenter defaultCenter] addObserver:aObserver selector:aSelector name:*notification_name* object:nil];
[[NSNotificationCenter defaultCenter] postNotificationName:*notification_name* object:parameter];

It tells different types of notification based on different notification.object, which is a custom subclass of NSObject which just contains some integers, some strings and some objects like

@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;
}

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.

Can anyone give some other advice ? Any other bad aspect of this architecture ?

Was it helpful?

Solution

I think it's a very very bad approach.

Every argument in you comment is right:

I feel that it is not a very good idea because there is no type checking for the notification.object.

of course.

And the notification based architecture is a broadcast system because it uses the same name for all notifications.

Unless intended, that makes absolutely no sense.

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.

Yes, it's wasting memory even though no memory warnings are fired. UIViews and UIViewControllers have their own lifecycle, and the SDK provide you with the methods to load and unload (alloc and release) resources just when you have to.

Apart from that, communications between controllers should be done through @propertys, protocols or any other methods provided by the SDK.

From Apple docs:

An observer of a given notification may be in a suspended state and not processing notifications immediately.

So you cannot rely on them to perform, let's say, crucial tasks.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top