Archive for November, 2012

Why flush ?

Interesting stuff i came to know about which i was unaware of. This post is more of a discussion on the problem. I haven’t understood the solution and so would request you to have a say.

I have done json parsing numerous times. I can tell if json is valid or not by looking it. I was using google-gson for android application and its awesome. Application was supported by php web service. The solution that fixed the problem is related to php and i am not a php developer and don’t have that much inner understanding of it. It was regular json parsing routine. Application just crashed when gson was parsing the json response from php service.

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 2

From error I understood the reason but that was not it. I was very confident that something else was the problem because json response was perfect. I wrote quick objective-c routine(SBJSON) to parse same json response and it worked there. I even did validation on jsonlint.com
After doing bit of research I came across a working solution. In php before sending response I had to put this:

echo header('Content-Type: application/json');

I checked the php manual to understand what those lines means, header function was quite obvious. According to manual, ob_clean:

This function discards the contents of the output buffer.

Now I have no idea why discarding the contents of output buffer was working. This was the case for android, I mean in iphone I didn’t need ob_clean. I am still looking for the explanation on this.

Affair with core data: IPhone

I have lots of stuff to write upon. Problems that I have faced and how I have solved it. For this post i have picked up coredata topic. Core Data is a persistence framework. I would love to give one beginner’s tutorial on coredata but this post will be about the pitfall that most of the beginners falls into and i have tasted it. I assume that you have already used Core data with your project. I would refer this tutorial for quick basic walkthorough. There was a chat application where i had to manange chat history. I was using Core Data for saving all chat messages. This is how my rough sketch of chat entity looks like:


@interface Chat : NSManagedObject

@property (nonatomic, retain) NSNumber * chat_id;
@property (nonatomic, retain) NSString * message;
@property (nonatomic, retain) NSNumber * timestamp;
@property (nonatomic, retain) NSString * username;


Chat entity has four attributes. Everything was setup properly. When I ran the program, an error occured:

Failed to call designated initializer on NSManagedObject

I was initializing my chat like this:

Chat *chat = [[Chat alloc] init];

According to error init wasn’t appropiate initializer. I checked the class reference docs and according to it:

if you instantiate a managed object directly, you must call the designated initializer (initWithEntity:insertIntoManagedObjectContext:)

My chat implementation file looks like this:

#import "Chat.h"

@implementation Chat

@dynamic chat_id;
@dynamic message;
@dynamic timestamp;
@dynamic username;


I looked for initWithEntity in refrence doc and it says:

You are discouraged from overriding this method—you should instead override awakeFromInsert and/or awakeFromFetch (if there is logic common to these methods, it should be factored into a third method which is invoked from both). If you do perform custom initialization in this method, you may cause problems with undo and redo operations.

Then I checked for NSEntityDescription in reference because initWithEntity was taking it as a parameter:

- (id)initWithEntity:(NSEntityDescription *)entity insertIntoManagedObjectContext:(NSManagedObjectContext *)context

I was going through all the methods in class reference and there I found one method:

+ (id)insertNewObjectForEntityForName:(NSString *)entityName inManagedObjectContext:(NSManagedObjectContext *)context

According to reference docs:

Creates, configures, and returns an instance of the class for the entity with a given name.
Returns a new, autoreleased, fully configured instance of the class for the entity named entityName. The instance has its entity description set and is inserted it into context.

From reading those, I changed my initializer and new code looked like this:

Chat *chat = (Chat *)[NSEntityDescription insertNewObjectForEntityForName:@"Chat" inManagedObjectContext:managedObjectContext];

This worked smoothly for me.

Reverse geocoding: IPhone

There will be a time when we would have to get the address of location from lattitude and longitude value. This concept is called Reverse geocoding. Well as usual this post is again about the mistake that I made and the solution that I got for it. Apple has introduced CLGeocoder class from ios 5.0 onwards. According to reference doc:

The CLGeocoder class provides services for converting between a coordinate (specified as a latitude and longitude) and the user-friendly representation of that coordinate.

For reverse geocoding there is a method where you provide location and it will convert:

- (void)reverseGeocodeLocation:(CLLocation *)location completionHandler:(CLGeocodeCompletionHandler)completionHandler

Now I’ll start the boring tale of my mistake. If you see the reverseGeocodeLocation, it takes CLLocation as parameter which represent location data that also consist our latitude and longitude information. Now I had just lat and long value that I was receiving from my json object. So I had a simple requirement to parse those values and convert them to meaningful street address. I have a float values of lat and long, reverseGeocodeLocation takes CLLocation as parameter. I need to somehow convert float values to CLLocation or make CLLocation from my float values. So here is what I did first:

CLLocation *coordinates = [[CLLocation alloc]init];
coordinates.coordinate.latitude = [@"41.409261" floatValue];
coordinates.coordinate.longitude = [@"2.163517" floatValue];

But there lies a big problem. The thing is coordinate property(CLLocationCoordinate2D) is read only so I cannot assign my float values to respective lat and long directly. So now what I had to do was initialize CLLocation from CLLocationCoordinate2D which is a structure. This is how I represented my lat and long with CLLocationCoordinate2D:

CLLocationCoordinate2D coordinate;
coordinate.latitude = [@"41.409261" floatValue];
coordinate.longitude = [@"2.163517" floatValue];

Initialize CLLocation with newly created coordinate:

CLLocation *currentLocation = [[CLLocation alloc]initWithCoordinate:coordinate altitude:1 horizontalAccuracy:1 verticalAccuracy:-1 timestamp:[NSDate date]];

My location is ready now and I just pass it to reverseGeocodeLocation and on complete handler I receive my meaningful street address:

[self.geocoder reverseGeocodeLocation: currentLocation completionHandler:
     ^(NSArray *placemarks, NSError *error) {      
         //Get nearby address
         CLPlacemark *placemark = [placemarks objectAtIndex:0];
         NSString *addressName = [[placemark.addressDictionary valueForKey:@"FormattedAddressLines"] componentsJoinedByString:@", "];

         NSLog(@"My location name is %@",addressName);

Another not so interesting tale…Lots of thing you learn from mistakes and lots of knowledge you gain. When I started iphone development, I chose non-ARC way to know and learn the concepts of memory management. It was fun and good experience apart from objective-c being ugly(syntax). Whenever there was memory leak and any crash due to low on memory, it used to give me dose of excitement, debugging tits and bits had been good learning experience. It has been good journey for me. I started ARC later and life’s been good and easy for me.

I was trying to do some experiment with one iphone project based on facebook. I remember when my project was already using SBJSON for parsing json, then later when I had to integrate facebook to my project and I had lots of sbjson conflict errors. It seemed facebook was using another version of SBJSON library, so I had to remove my SBJSON and integrate the library that facebook was using. I found it in github facebook project. So that was another experience and at that time I was working on non-arc environment. So recently I tried to do something to see the result on what would happen if I do along these lines. So I created ARC based project and integrated facebook sdk 3.1 and then I integarted that same SBJSON that I had integrated on past to avoid conflict. This time my project and facebook sdk was ARC based and SBJSON was non-arc. So I decided to see the result by changing all lines of releasing memory to setting them to nil on the source codes of SBJSON. As release call will throw syntax error in arc environment so I was replacing them by nil assignment. Why nil assignment ? Well I wanted to see if there is any harm with that?

Everthing was fine. I was making rest call to my web services and parsing json. Then moment came and one crash sting me like a bee. So my crash error was this:

MyApp(2121,0xb0185000) malloc: *** error for object 0x1068310: double free
*** set a breakpoint in malloc_error_break to debug

From the description it was clear that I was trying to release already released memory. But big question was WHERE? Then as error asked me to do, I did set a breakpoint on malloc_error_break but still it was not enough for me. I need more clear error log, so I enabled zombies and trust me!! at this point of time zombies is your best friend. So as I knew I got a very clear picture, yeah NAKED CLEAR

[NSDecimalNumber retain]: message sent to deallocated instance

So it seems my decimal number has already been released before retain was called. Now another big question was NSDecimalNumber ????

I decided to play with parsing json because this crash was coming only parsing certain type of value. So I need to dig up more deeper. I was parsing every kind of value and trying to replicate the crash. Then crash happened and I had a big stupid grin on my face. I again tested parsing same kind of data and crash happened again and my stupid grin went wider. So crash was happening when I was parsing numbers. Something like this {“answer”:5} Now time was to follow the trails. So I started from [responseString JSONValue] , responseString is string and JSONValue is a method added via category. So I opened NSSting+SBJSON.m file and looked for the function. Now within function SBJsonParser was being used to parse the string, so I opened SBJsonParser.m file and searched for NSDecimalNumber because my best friend zombie told me I had a memory boom with decimal numbers. I got goosebumps when I found the stupid but very critical mistake I had made and it was the culprit of existence of this post that you are reading now. 😀 So my mistake happend on this method

(BOOL)scanNumber:(NSNumber **)o

See this is why I love zombie as it told me the problem was with decimalnumber and see where did I end up with. Yeah scanNumber! 😛 Now time for showing my mistake, but I would also like you to check the source code and look by yourself and I guess you already have SBJSON library.

@autoreleasepool {
    id str = [[NSString alloc] initWithBytesNoCopy:(char*)ns
                                            length:c - ns

    if (str && (*o = [NSDecimalNumber decimalNumberWithString:str]))
        return YES;

and original lines of code was like this:

id str = [[NSString alloc] initWithBytesNoCopy:(char*)ns
                                            length:c - ns
[str autorelease];
if (str && (*o = [NSDecimalNumber decimalNumberWithString:str]))
    return YES;

My mistake was that I used @autoreleasepool block because in hurry I couldn’t see that in original source code NSAutoreleasePool was not used and when I was using @autoreleasepool then all the autoreleased object was getting released after finish of the block. In my case decimalNumberWithString got released after releasepool block was finished and YES carriage was return. So later *o was pointing to deallocated memory and my app was crashing whenever I was parsing numbers.

But this was worth it and lots of fun debugging this crash. Also I am posting screenshot of enabling zombie objects

Facebook authentication error: Iphone

Facebook has released new 3.1 version of sdk for ios (and ios 6 compatible). You can target ios 5 also, just need to do small tweak during setup. They have details and tutorial on developers’ site. This post is not about any how to’s, here I will be talking about another few things that you need to be aware of and take care of it. I have integrated facebook sdk with android and iphone projects many times, but you can fall into one basic pitfall if not taken care of.

So your facebook app is ready. You have its app id and facebook sdk is well integrated with your project. You are ready to go. Now during facebook authentication from your app it gives this error (screenshot

the operation couldn’t be completed com.facebook.sdk error 2
Now you will be thinking what went wrong? You’ll double check the facebook app id, and go through all settings you made and see if you have missed anything? But you find that everything is in its place. So what the duck is wrong with the program? What we are missing out is one basic setting during facebook app creation. If you check the facebook app dashboard again and look for native IOS App section. There you will find Bundle ID field, here you will have to enter your application bundle identifier so that facebook knows which app to redirect back to once auth is complete. This is very safe and you should provide this field on dashboard setting. Your application might run without that detail as I have tested this and confirmed it too that it has run on many other application without that setting but then in some devices/scenario it will definitely hit you. Your simulator and testing device might be running without issue but when you distribute it to some other device then you will face it eventually. So just be on safe side and provide your bundle identifier to your facebook app settings.