Trying to learn Ruby and rails

Its been a short while i have taken up learning ruby on rails as part of my work in the company. Its always been a item on my to-do list, Learn a web development framework and procrastination helped me drag along not doing it till this time.

However having basically programmed with traditional languages like C and a insanely easy development framework for iphone using Objective-C, i must tell you my initial reaction of learning ruby on rails.

It looks like ruby was developed to mock the other programming languages. Though its very easy to read, it will take time for one to master it. These first few days where i have been learning ruby as a pre-requisite for rails, i felt the real power can be extracted when you know the language framework in and out. It provides you a very high level of abstraction and the code looks like you have written an essay in english (easy to read).
Sample this:
3.times print "Ruby" I was like where is the loop. There are many other wow moments while learning it.

Rails is a beautiful framework which has totally abstracted the db from the developer. All one needs is to use the interface provided by it to do all the DB operations. The well structured directory structure and the well defined naming conventions make take a bit of time to sink into my system, but the simplicity makes me admire the framework. It follows a pure MVC architecture and configurations play a important role in it.

This is just a small write up on my initial reaction after few days of taking up the RoR framework. Hope to write more as i learn and dig deep into it.

How to use facebook connect in your iOs App?

As facebook has gained immense popularity it has become essential for iOs app developers to cash in on this opportunity by integrating the facebook connect feature in the application thereby avoiding the hassles of having user sign up to a new service.

Facebook has provided a great SDK which is easy to use and can be integrated into a iOs App with minimum hassles. In this post i will explain about the basic steps needed to integrate the Facebook SDK and make a sample Graph API call.

First of all we need to register/create a app on developer.facebook.com inorder to get the credentials which will be used in the SDK to prompt users to authorize the application to FB data from their account.

Next the facebook SDK can be downloaded from the git repository at https://github.com/facebook/facebook-ios-sdk .

There is a sample application which demonstrates how to use the SDK. However i will be explaining the basics in this blog post.

Drag the Facebook SDK folder onto your xcode project and copy the files. Now create a Button item in your application and a corresponding IBAction method in your controller.

Now get the list of permissions you want the user to grant to your application and create a NSArray of permissions which will be used with the SDK.

In your controller’s header file import the facebook.h header and implement the two delegates whose methods will be called once we initiate the connect process.

#import "Facebook.h" // Also implement the FBSessionDelegate and FBRequestDelegate
@interface myViewController {
Facebook *facebook;
}

In the view controller’s IBAction the facebook object can be then initialised as shown below:

-(IBAction) faceBookConnectPressed{

NSArray *permissions = [[NSArray arrayWithObjects:@"email",@"user_location",@"offline_access",
@"publish_stream",@"read_stream",@"user_about_me",nil] retain];
facebook = [[Facebook alloc] initWithAppId: andDelegate:self];

}

At this point the facebook object will either launch the login dialog box or open the safari browser for authentication. If you want the dialog box to open always, then you can change the following in facebook.m.
[self authorizeWithFBAppAuth:YES safariAuth:YES]; to [self authorizeWithFBAppAuth:YES safariAuth:NO];

FB authorization dialog

Once you login to facebook, it will present the dialog box to authorize the app with the permissions specified. If the user clicks allow, the delegate method will be called which returns the access-token. The accesstoken is part of the facebook object created and it can then be used to make subsequent GRAPH API calls.

The delegate methods called here are

- (void)fbDidLogin {
}
-(void)fbDidNotLogin:(BOOL)cancelled {
}
- (void)fbDidLogout {
}

The access token can then be retrieved from the facebook object created in our IBAction method.
Using the object the GRAPH API calls can be made and the delegate methods can be used to handle the responses.
Inorder to get the user profile, one can call the graph APIs as follows:

[facebook requestWithGraphPath:@"me" andDelegate:self];

The delegate method will then return the response in form of NSData*

- (void)request:(FBRequest *)request didLoadRawResponse:(NSData *)data
{
    NSString *xmlLoginResponse = [[NSString alloc] initWithBytes:[data bytes] length:[data length] encoding:NSUTF8StringEncoding];
	NSLog(@"%@",xmlLoginResponse);
}

This can then be converted to JSON and values can be fetched as a NSDictionary.

This concludes the basics of using the facebook SDK. I will update on how to post on wall in a new blog post.
Do drop a comment if you want additional information on the same.

The list of Graph APIs can be found here

My take on memory management in Objective-C

Memory management in objective C remains to be one of the most important concepts while developing on iOs. Unlike Java where the garbage collector clears off un-held references, in objective it has to be done in code.

I would like to mention some of the basic techniques used for memory management in Obj-C.

Alloc and new

When a new object is created using a alloc and new methods the reference count is incremented by one. However if the returned object is assigned to a property retain the reference count increments by one. For example consider the following code.

UIImageView *myImageView = [UIImageView alloc];// Allocates memory and the reference count is 1
myImageView.image = [UIImage imageNamed:@"myImage.png"]; // Assigns a autorelease object as alloc or new is not used
[myImageView release]; // decrements the reference count by 1

The first line allocates memory to the imageview object thereby increasing the reference count to 1. When a release message is sent to the imageview the memory is deallocated and released as the reference count becomes 0 and there are no more objects holding the reference to this memory location.

When a allocated object is assigned to a variable with a property retain, the reference count increases by 1. Consider the following example:

@property (non-atomic, retain) UIImageView *retainedImageView;

When this is allocated memory the reference count goes up to 2 which has to be taken care of.

self.retainedImageView = [ UIImageView alloc]; // reference count is actually 2 here. alloc increments it by 1 and retain increments it again by 1.

Usually in such scenario its better to declare a local variable and then assign the class variable to it as shown below.

UIImageView *myImageView = [UIImageView alloc];// Allocates memory and the reference count is 1
self.retainedImageView = myImageView; // Reference count is now 2
[myImageView release]; // reference count is now 1

Usually objects for variables with property retain are auto release objects.

However care must be taken to send a message nil to the object in dealloc to completely clear the memory.

self.retainedImageView = nil;
Note on auto released objects:

An auto released objected has to be released only if retained by the user of that particular object. If the object is of type auto release and the variable holding it does not retain the value, then after the scope of the method is over, the objected is released into the auto release pool and there is no memory leak.

Some of the consequences of bad memory management are:

    Memory leaks
    Memory warnings
    Crashes

There could be more, but the one’s listed here are the most frequent.

Delegate pattern in Objective-C

Delegate pattern is a design pattern in object oriented paradigm where a object delegates the performing of an action to another object. The receiving object acts as a delegate of the sending object. This is useful especially in scenarios when some action is to be performed by an object which is waiting for another object to perform a task.

In objective C it help pass message between objects such that the action can be performed by the delegate instead. Consider a scenario where a view controller has presented presented a tableview as a modal view and waiting for the selection to perform a new action. When it presents the uitableview it can assign itself to be the delegate of the uitableview’s viewcontroller. Once the selection is complete on the uitableview it can call the delegate method with/without data which will then start the action to be performed.

A delegate pattern can be implemented as follows:

A class which wants to delegate an action to another object has to define a protocol with the delegate name

@protocol ClassADelegate // This is forward declaration of the protocol

@interface MyClassA
{
     id  delegate;
}
@property (non-atomic, retain) id  delegate;
@end
@protocol MyDelegate
- (void)myDelegateMethod:(id)object; // The method signature depends on the data you want to send to the delegate
@end;

Once done, you have to synthesize the delegate member in the .m file by

 @synthesize delegate ; 

MyClassB which wants to be the delegate of the MyClassA can implement the delegate in its interface as follows:

In the header file import the header for MyClassA and inform the compiler that it will implement the ClassADelegate by adding it in between the <>
brackets.

@interface MyClassB 
{
// member variables to be defined here
}

The .m file should now implement the delegate method myDelegateMethod as follows:

- (void) myDelegateMethod:(id)data
{
// Use the data and perform any action here like updating the UI, sending request to a remote server etc
// The signature for this method can be defined to take multiple or no parameters based on the requirement
// This method should be implemented in the class which wants to act as a delegate, and if not done may lead to exceptions when the method is called

}

When the object of type MyClassA is created in MyClassB, it has to assign the delegate to itself.

Thing to be noted here is , any object can assigned as delegate provided it implements the delegate method.

MyClassA *objectA = [[MyClassA alloc] init];
objectA.delegate = self; // Any other object can be assigned as delegate as long as it implements the delegate method

Finally when the objectA wants to send a message to its delegate , it can call the delegate method as follows:

[delegate myDelegateMethod:nil]; // Note you can pass any number of objects to the delegate method here

Thus implementing a delegate helps achieve a easy way of communicating between objects in objective C.

If you have any question please leave a comment and i will address it.

The web

Original post can be found here.

Its all relative

           
That looks great

Now?

Well i really liked the author’s idea of presenting the graph which has totally opposite effects here. For the entire post click here.

Anger management

Funny

Calvin

Attaining satisfaction

Isn’t it true about the education system we have?

Hello world!

Finally a place to share my thoughts on random things.