Dealing with Google App Engine development datastore.
I’ve been using GAE for a couple of years now, and I’ve become pretty much accustomed to its foibles, except for one: it occasionally wipes out my development datastore for no apparent reason. It always happens when I have to restart the dev. server application, and it usually happens when I switch between two of my applications. But whatever the cause, it can be really annoying. So I’ve found a workaround of sorts.
Digging thorough the development server documentation, I found two options that are making my life much easier:
- –use_sqlite
- Use sqlite for the local datastore.
- –datastore_path=[path to datastore file]
- Keep the datastore in the file specified.
By doing these two things, you can now see where GAE is keeping your datastore and you can make intermediate backups and restore them at will. You can also view your datastore using the command line sqlite client. Just add these two parameters to the Extra Flags box in your app’s info in the App Engine Launcher (or to the command line) and you’re good to go. Enjoy!
My Review of iOS 4 Programming Cookbook
Originally submitted at O’Reilly
You can build a variety of amazing apps on the iOS platform—and every one of them presents a unique set of problems. With the recipes in this cookbook, you'll go beyond theory to solve the vexing, real-life issues you’re likely to face when creating apps for the iPhone, iPad, or …
Chock full of code.
Pros: Helpful examples
Best Uses: Intermediate, Expert
Describe Yourself: Developer, Educator
Disclosure: I’m an O’Reilly Author and developer of the Great iPhone Development Video series. That being said, I’m not one for pulling punches when I see issues with with people’s code (ask anyone I’ve ever code reviewed
.
This is the book I wish I’d had when I started developing for the iPhone. I started writing apps about two weeks after the infamous Apple Developer NDA was lifted and information started trickling out onto the Internet. If I’d have had a book like the iOS Cookbook I could have saved myself many hours of painful trial and error while learning Objective C and what is now the iOS API.
This is not really a book for a beginning iOS programmer. It’s a book for someone who’s done a couple of simple apps and has the basic idiom down. If you’re looking to learn Objective-C or the mechanics of writing an iPhone app, this book will not help you. But if you can already write a functional app, the code snippets in this book will trim lots of time off of your learning curve when it comes to implementing more sophisticated features like Core Data, gestures, etc.
There are a few areas where the examples could be clearer, and it’s clearly impossible to cover some of the more sophisticated functions of areas like Core Data in 620 pages. But overall this is an excellent REFERENCE for new and experienced app developers alike, and I’d recommend adding it to your library.
(legalese)
iPhone Core Data/undo gotcha of the day.
In general, I like the NSUndoManager functionality in iOS, but sometimes the secret handshakes you need to know can really get me down. For example, I needed to disable undo/redo when setting a particular property of a model entity. Reading the documentation, this seemed pretty straightforward:
[[theApp.managedObjectContext undoManager] disableUndoRegistration]; detailItem.fieldImageFile = relativePath; [[theApp.managedObjectContext undoManager] enableUndoRegistration];
Seemed very clear, but it didn’t work! So, after much poking around on the Internet, I found a posting that alluded to the fact that changes to undo don’t occur until the run loop executes. So, to get the desired effect, I ended up with the following code:
[[NSRunLoop currentRunLoop] runMode: NSDefaultRunLoopMode beforeDate:[NSDate date]]; [[theApp.managedObjectContext undoManager] disableUndoRegistration]; detailItem.fieldImageFile = relativePath; [[NSRunLoop currentRunLoop] runMode: NSDefaultRunLoopMode beforeDate:[NSDate date]]; [[theApp.managedObjectContext undoManager] enableUndoRegistration];
Yet another head-scratching API decision from Apple.
Key-value Observer (KVO) on the iPhone.
Here’s one for the you-learn-something-new-every-day file. Objective C supports a key-value-observer model that lets you monitor changes on an object’s property values. Unfortunately, I found this out the hard way while working on a MKMapKit project.
What I wanted to do seemed simple: have a map view with a bunch of moving objects on it. Each object pulls its current position from an XML document on the web. I already had my XML-wrapper object written when I wanted to display it on the map, so I simply implemented the MKAnnotation protocol. All of my objects appeared, and all seemed to be right with the world.
Then, I started updating my objects and found that the annotations didn’t move. At all. I found a bunch of people on the web trying to solve the same problem, but I wasn’t very happy with the solutions I found. Most of them involved removing the annotations and adding them back into the map view. But, thankfully, I found one article that mentioned KVO on the iPhone. I was pretty familiar with KVO from my Flex projects, but didn’t even realize it was supported in Objective C.
To make a long story short, if you don’t use the willChangeValueForKey and didChangeValueForKey before and after updating your coordinate property on your annotation object, the MKMapView will not be aware that it has moved. After bracketing my XML update code with those two calls, all of a sudden my annotations started moving around.
-(void)xmlProperty:(NSString *)newXml
{
[self willChangeValueForKey:@"coordinate"];
[xml release];
xml = [newXml retain];
[self didChangeValueForKey:@"coordinate"];
}
Exporting from Core Data on iOS.
So, I’m working on the latest release of our scrapbooking app (Coolibah) and I needed to export some objects that are stored in SQLLite through Core Data to my server. After googling around for a while (and finding not much, it seems like search results have started to really suck lately) I had to strike out on my own and work something up. I got to use a very cool Objective C feature that I’ve only recently discovered: categories.
I was able to extend the NSManagedObject class and add a new xmlString property. By using the entity property to do some simple introspection, I was able to write a serialization function in about 50 lines of code. Enjoy!
NSManagedObject+XMLSync.h
// // Created by Scott Means on 1/5/11. // Released into the public domain without warranty. #import <CoreData/CoreData.h> #import <Foundation/Foundation.h> @interface NSManagedObject (XMLSync) @property (nonatomic, readonly) NSString *xmlString; @end
NSManagedObject+XMLSync.m
//
// Created by Scott Means on 1/5/11.
// Released into the public domain without warranty.
#import "NSManagedObject+XMLSync.h"
@implementation NSManagedObject (XMLSync)
- (NSString *)xmlString
{
NSEntityDescription *ed = self.entity;
NSURL *uri = self.objectID.URIRepresentation;
NSMutableString *x = [NSMutableString stringWithFormat:@"<%@ id=\"/%@%@\"",
ed.name, uri.host, uri.path];
for (NSString *a in ed.attributesByName.allKeys) {
id value = [self valueForKey:a];
if (value) {
if ([value isKindOfClass:[NSString class]]) {
[x appendFormat:@" %@=\"%@\"", a, value];
} else {
if (![value respondsToSelector:@selector(stringValue)]) {
NSLog(@"no stringValue");
}
[x appendFormat:@" %@=\"%@\"", a, [value stringValue]];
}
}
}
bool hasChildren = NO;
for (NSString *r in ed.relationshipsByName) {
if (!hasChildren) {
[x appendString:@"/>"];
hasChildren = YES;
}
NSRelationshipDescription *rd = [ed.relationshipsByName objectForKey:r];
if (rd.isToMany) {
hasChildren = YES;
[x appendFormat:@"<%@>", r];
for (NSManagedObject *c in [self valueForKey:r]) {
[x appendString:c.xmlString];
}
[x appendFormat:@"%@>", r];
}
}
if (hasChildren) {
[x appendFormat:@"%@>", ed.name];
}
return x;
}
@end
Next project: figure out how to do better code formatting in WordPress!
