How to find that sqlite file behind your CoreData

This is a quick thing. Hard to find unless you know where to look. When running a CoreData driven app, there are times when you’d like to just rest your data store and start again. Doing a schema change is one such case. Of course if you’re doing something really silly and mess up the data, again.. reset!

So, to do that, simply delete the .sqlite file for your app. Running in the simulator, that is stored in your home directory under ~/Library/Application Support/iPhone Simulator/User/Applications/<special app id>/Documents/<appname>.sqlite

Easy, huh? Deleting this is sure nicer than clearing all apps and data from the simulator.

WWDC09 Keynote

I got up before 6am and got into the line about 6:40. By then, I was #722. I heard the first person in line got there at 5:30pm the yesterday! That’s hard core! I might have broken the 700 barrier if I hadn’t stopped at Starbucks to get oatmeal and a frappuccino. I’m pretty sure I saw Peter Ha from CrunchGear getting into line after me! You can see from the pictures below, how long the lines were and how packed in we were while waiting inside for the 3rd floor to open up.

A great summary of the keynote is posted on Engadget as well, so I won’t try to duplicate that.

Finding freed/deallocated instances of objects

This procedure has saved me more than once, so I need to document it throughly for everyone! There are cases where I see messages (in the debugger console) that are like this;

-[NSFetchedResultsController class]: message sent to deallocated instance 0x11957d0

Sometimes, it will talk about “freed” instead of deallocated. That address at the end is important. To track down the line of code where the object was allocated, you’ll need to set a couple environment settings. To do this, right click on the executable you’re trying to debug and select “get info”. Select the “arguments” tag and set the values shown below;

 

debug environment settings

debug environment settings

(be sure to disable these when you’re done. They should not be enabled for a production build!)

Next, run your program and test enough to generate the error. In the Debugger Console, copy that hex address value, then type the command;

(gdb) info malloc-history <paste-address-here>

 

You should see something like this;

 

Stack – pthread: 0xa0416720 number of frames: 28

    0: 0x9264382d in malloc_zone_calloc

    1: 0x92643782 in calloc

    2: 0x93611618 in _internal_class_createInstanceFromZone

    3: 0x9361ab08 in _internal_class_createInstance

    4: 0x3020275c in +[NSObject allocWithZone:]

    5: 0x3020264a in +[NSObject alloc]

    6: 0x3d92 in -[AMRAPViewController fetchedResultsController] at /Users/dkavanagh/CrossFit Timer/Classes/AMRAPViewController.m:146

    7: 0x3a18 in -[AMRAPViewController viewWillAppear:] at /Users/dkavanagh/CrossFit Timer/Classes/AMRAPViewController.m:96

    8: 0x3097c945 in -[UINavigationController _startTransition:fromViewController:toViewController:]

    9: 0x30977c33 in -[UINavigationController _startDeferredTransitionIfNeeded]

   10: 0x3097d01e in -[UINavigationController pushViewController:transition:forceImmediate:]

The first line below the object alloc (in my case, line 6) will show where the offending object was allocated and should really help in tracking down the free/dealloc problem.

The original solution was posted in the app dev forums. You’ll need a login to see this.

Simple UIViewController Management

I’m writing a navigation based application for the iPhone and need to instantiate the same UIViewController (for a screen) in more than one place. In some sample code I’ve seen, local copies are kept in an NSArray. I’ve been doing that myself until I decided the Factory design pattern was probably more useful. I wrote the code below and it has been working very well.

#import <Foundation/Foundation.h>
@interface ControllerManager : NSObject {
NSMutableDictionary *controllers;
}
+ (ControllerManager *)controllerManager;
– (UIViewController *)controllerByName:(NSString *)name;
@end
#import <Foundation/Foundation.h>

@interface ControllerManager : NSObject {
	NSMutableDictionary *controllers;
}

+ (ControllerManager *)controllerManager;
- (UIViewController *)controllerByName:(NSString *)name;
@end

@implementation ControllerManager

+ (ControllerManager *)controllerManager {
	static ControllerManager *sharedInstance;
	@synchronized(self) {
		if (!sharedInstance) {
			sharedInstance = [ControllerManager alloc];
		}
	}
	return sharedInstance;
}

- (UIViewController *)controllerByName:(NSString *)name {
	if (controllers == nil) {
		controllers = [[NSMutableDictionary alloc] initWithCapacity:5];
	}
	UIViewController *ret = [controllers objectForKey:name];
	if (ret == nil) {
		Class cls = NSClassFromString(name);
		ret = [cls alloc];
		[ret initWithNibName:name bundle:nil];
		[controllers setObject:ret forKey:name];
	}
	return ret;
}

@end

If you’d like to push another view controller, do something like this;

[[self navigationController] pushViewController:[[ControllerManager controllerManager controllerByName:@"MyViewController"] animated:YES];

The first time the view loads, it is initialized from the nib. The nib must be named the same as the view controller, which by the examples I’ve seen is fairly common anyway. The view is then stored in the ControllerManager and served up as a singleton. Since you can write your view to load data before it is viewed, this pattern works well and saves memory by not allowing multiple copies of the views/controllers.

directEC2 is available in the AppStore

I’m pleased to announce that the application I wrote to manage Amazon EC2 instances from an iPhone or iPod touch is now in the AppStore. This application is the first version of what will become a very feature rich management console. Here is a quick run-down of the features;

  • Manage images, instances, volumes, snapshots and more.
  • Maintain launch configurations to quickly spin up more servers
  • Check server status, console output
  • Multiple account support
  • Access all regions
  • Create, attach volumes
  • Backup and restore with snapshots
  • Shake navigation aid
That last item is something I came up with to solve the problem of being several levels deep in the application navigation. To simplify returning to the top level, the application responds to shake and resets the navigation. This turns out to be pretty handy and I hope other applications adopt this feature.
Get the app here: iTunes App Store
Here are some screenshots to entice you;
img_0020img_0009img_0010img_0008img_0011

iPhone ushers us back to heavy-weight apps

OK, if you can call an iPhone app, heavy weight… Just when you thought the rest of the cool apps would all run in a browser, here comes the iPhone. Before this, I thought mobile apps were pretty silly and too much of a pain to distribute. I tried my hand at J2ME, but the carriers have distribution of those really locked down and it is hard for anyone at a grass roots level to get a J2ME app distributed. All of the cool features require special signing certificates that are very carrier specific. (damn you, mobile carriers!!)
In comes the iPhone. Here is a mobile platform that offers a very consistent set of features and has a very large installed base. It has a huge cool factor (yes, even this long after it originally launched). The app store really changed the model for distributing mobile applications. Now, anyone (given a $99 entry fee) can become an iPhone developer. OK, I’m hearing people say $99 isn’t all, because you need to invest at least $1400 (rough figure) for a development box and device. This is exactly what I started with, and 20″ iMac and iPod touch. OK, but you don’t have to be anyone special to sign up, that’s the point. Anyone with about 1500 bucks and a little skill, creativity and ambition can become an iPhone developer. By all accounts, a lot of people have! 800 million app downloads to 30 million devices and 25,000 apps available in the app store! Holy cow! Now, that’s a markeplace! Developers keep 70% of the revenue. Good for Apple, good for us.
Today, I was watching a video on crunchgear about the NIN iPhone app and think this really drives home something I’ve been thinking about for a while. People don’t bother doing a really nice web app that runs on the iPhone, they write an iPhone app specifically for their service/movie/band, whatever. I recommend watching the video, just to see the really excellent integration of features on the iPhone and then in their regular web site. Very cool stuff!
I wonder, did the lack of Flash on the iPhone help boost iPhone apps? I certainly didn’t hurt. With Flash, there are a lot of developers who would have been ready to build apps sized for the iPhone. There would have been some really nifty, media-heavy/interactive apps built and deployed over the web (no need for the app store). The one thing that would have been missing is the tight integration with iPhone OS features. Things like geo-location, access to camera/photos/music, accelerometer, etc. I think Flash would have meant fewer native iPhone apps, but the native apps would have been built anyway.
I say, welcome back to the client applications! With a well-managed distribution/update system in place, client apps can be managed very easily. The app store does that reasonably well. I just hope I can build the next iShoot!

iPhone 3.0 OS helps you login to hotspots!

I had my iPod touch that has the 3.0 OS loaded with me yesterday. I thought I’d try it on the Panera WiFi. I often go there to work with my laptop and get something to eat. Often, I meet someone there and we socialize and work and eat.. it’s all good.
Back to the 3.0 OS thing. The neat thing I discovered was that when I selected the “Panera” hotspot, I was presented with a browser window so that I could authenticate right away! That was pretty cool! It saves me from having to go into Safari, just for the purposes of clicking the “login” button.

Alert showing available hotspots

Alert showing available hotspots

Login helper window

Login helper window

(It was hard to get a shot of the browser window… this is the best I could do)
So, I login via that browser window, click “done” and I’m able to go into what ever app I wanted to run in the first place. I’m impressed because I always felt that was a nuisance and this new way is much more streamlined. Thanks, Apple!!

New iPhone 3.0 software has been announced!

Here are a few of my favorite things;
cut/paste
landscape keyboard
bluetooth peer-to-peer
in-app purchasing (think renewing subscriptions)
stereo bluetooth (A2DP)
search across all (or almost all) data in the phone

All in all, cool stuff! Developers can get their hands on 3.0 right now (or as soon as the dev site comes back!). To be released in “this summer”. Free to iPhone users, $9.95 for iPod touch (boo!!).

Editing xib files for rapid UI building

I just decided to look at what a .xib file contained. The “x” should have been the clue that it just might be an XML form of a .nib file. If you’re lost, I’m talking about the files that the Apple Interface Builder tool uses to store interface definitions.

I needed to copy an existing interface, then make minor tweaks. Starting from scratch in Interface Builder isn’t so bad, but I’m really a Vim guy of old and being able to fly through a text file and make edits is 2nd nature to me. If you’ve ever edited XML manually, this won’t seem like a silly notion. For that matter, you could use any of the XML editors out there as well.

To start with, I copied the existing .xib to a new name. In Vim, I changed the references to the old name to the new one. So, now my new interface also references a new controller class, which I’ll probably copy/paste also. It’s pretty trival to change the button text and IBActions and IBOutlets the .xib points to. There are a lot of reference numbers within the XML, so I wouldn’t do too many changes for fear of messing up some relationships that Interface Builder requires. Also, don’t forget to add (existing file) to your project, so that you build with the new interface def.

Happy .xib editing!