I am new to iOS programming.
Now I have a model 'Activity'. The activity needs to send and receive data from the server using AFnetworking library.
There is a Server Manager dealing with all networking methods.
My idea is to create a Server Manager instance when initialise each Activity object, and receive data. Therefore when I need a table of activities, I have to call the instance repeatedly when I create each activity
+ (ServerManager*) instance
{
static ServerManager *Instance = nil;
static dispatch_once_t onceToken = 0;
dispatch_once(&onceToken, ^{
Instance = [[ServerManager alloc]init];
});
return Instance;
}
It makes sense but is it valid or appropriate to repeatedly create server manager instance?
Or it is better to call instance while loading UI and assigning every data in viewdidload?
Thank you
There are multiple ways to do what you want to do, and it all depends on the situation and the amount of data you want to load. The way I do it usually is: I would rather extend your Activity class from the ServerManager class, so that all the network methods available in ServerManager are automatically available to the Activity class.
So this way, each time you create a new Activity class, it already has the capability to go out and get data for you.
Again, this is subjective and just the way I usually do it.
Hope this helps.