Reputation: 5413
I have a noob question regarding how to architect my iPhone app.
I have two main components, a map and an audio player, both self contained classes (MapController and AudioController). I read this great article and am trying to apply it to my app:
http://blog.shinetech.com/2011/06/14/delegation-notification-and-observation/
I have a third class, WebServices, that handles uploading POST data to my server as well as making queries to external API's.
My question:
In particular, I'm interested in which approach consumes memory more efficiently. Or if it doesn't matter at all.
Thanks!
Upvotes: 2
Views: 120
Reputation: 104698
Point Zero
Do not resort to singletons, and especially not in multithreaded contexts. You can search this site if you need explanations (hint: the reasons against them are quite similar in OO languages similar to ObjC. e.g. C++, Java, C#...).
To share, or not?
It ultimately depends on the behavior your design needs, but I favor Option #1 as the default.
WebServices
may be huge). Smaller objects allow more flexibility and reduce the chances that your program will compete for resources.Assume multiple instances (1) until have identified a problem. Even after the problem is identified, you can usually avoid sharing.
To determine which consumes less memory requires much more information. If the objects you are considering sharing are not huge (very unlikely), they themselves are not problems wrt memory consumption.
Either approach proposed can result in more memory consumption - it depends entirely on the context and execution. We'll need some more information to answer this specifically.
This is not to imply that sharing is bad, but sharing usually complicates things because you need to design objects to behave predictably when shared (especially since you are operating in a multithreaded context).
Ask yourself:
WebServices
provide?WebServices
improve your program and reduce complexity?It's likely the case that the WebServices
map stuff does not interact with the audio stuff. In that case, WebServices
may be a good candidate for a base class, where the audio and map services are separate subclasses (assuming the WebServices
class is already complex).
Upvotes: 0
Reputation: 12107
I would suggest an NSOperationQueue
with a number of NSOperation
s, instead of a WebServices singleton.
Look at this sweet example from Apple where they do this for concurrent image-downloading. It's a Mac example, but all the classes shown are available on iOS. They use NSInvocationOperation
, but you can use an NSOperation
subclass, or an NSBlockOperation
for cleaner code.
You can add these from anywhere to a single queue, by calling [NSOperationQueue mainQueue];
to fetch the main queue singleton. The singleton's job, in this case, is only to manage the queue (so not too many downloads happen at once), not to do any data processing (that's all handled by your NSOperation
).
Upvotes: 0
Reputation: 32066
Consider creating a singleton for your WebServices class and use a shared instance. It seems this is the design pattern you require here.
With regard to the efficiency part of the question, the second option is more efficient purely because you're not storing as much data in RAM. The difference however, assuming your classes are not storing too much internally, is likely to be unnoticable.
@interface WebServices: NSObject
{
}
+ (WebServices*)sharedInstance;
@end
static WebServices *sharedInstance;
@implementation WebSerivces
+ (WebServices*)sharedInstance
{
@synchronized(self)
{
if (!sharedInstance)
sharedInstance = [[WebServices alloc] init];
return sharedInstance;
}
}
@end
Upvotes: 2