As we have mentioned in our previous post, manual reference counting is used to manage memory and avoid premature deallocation and memory leaks.
Retain Counts
An object never knows who its owners are. It only knows its retain count – i.e. how many times it has been retained in memory.
When an object is first created, it has one owner and hence its retain count is set to 1. When an object gains an owner, its retain count is incremented. When an object loses an owner, its retain count is decremented. When the retain count reaches 0, the object sends itself the message dealloc, which returns all of the memory it occupied to the heap.
If we are to write our own code to represent this logic, it would look like:
- (id)retain
{
retainCount++;
return self;
}
- (void)release
{
retainCount--;
if (retainCount == 0)
[self dealloc];
}
Read the rest of this entry »
Memory management in the Cocoa Touch framework is one of the trickier concepts to understand for new developers. Unlike the Cocoa framework for Mac apps, Objective-C on the iPhone has no garbage collector. It is the developer’s responsibility to manage the memory in the application.
Basic Concepts
The iPhone has a limited amount of RAM. When iOS launches our app, it reserves a heaping pile of unused RAM for our app. The memory that our app has to work with is simply called the heap. The heap is isolated specifically for our app and our app can do whatever it wants with it without affecting the rest of the operating system or other apps.
Read the rest of this entry »
One of the simplest way to implement a user login mechanism for your iPhone app is to leverage on facebook’s pre-written fb iOS SDK. With more than 100 million users using the facebook app on various mobile devices and 68 million+ monthly active iPhone app users at this time of writing, it is almost a sin for an iPhone app developer not to leverage on this and use facebook’s authentication mechanism for his/her app. I am sure there are exceptions to the rule but for most of the app developers out there, it simply makes sense to use facebook’s login mechanism, especially when facebook’s iOS SDK is well-documented and easy-to-use.
Getting Started
To get started, we can grab the SDK from Github:
calvin$ git clone https://github.com/facebook/facebook-ios-sdk.git