CoreData SQLite3 Database Constraint Failed Error

Have you ever seen the following error message:

Error Domain=NSCocoaErrorDomain Code=256 UserInfo=0x3baae90 "Operation could not be completed. (Cocoa error 256.)

Talk about not Helpful.

Inside this there was a userInfo dictionary with the following message:

error during SQL execution : constraint failed

Slightly more helpful, but not by very much.

We came across this error message while we were developing our Electrophobe game. We started the debugging process by inserting logging code and pinpointing where the code was failing out. After about 6 hours of work combing through both code and rebuilt databases we finally found the solution.

The trick was that the primary key store for the SQLite3 database that was backing our CoreData NSPersistenceStore had an incorrect value for the Z_MAX column. This column is used, we believe, to house the maximum primary key in order to quickly retrieve it for additional inserts.

As is evident from our escapade however, it is clearly a little vulnerable to tampering when loading the data directly into the SQLite database. To fix the issue, simply update the table with the max(Z_ID) from the referenced table and it should all hook up again.

Tags: , , , ,

Posting Multiple Files With ASI

ASI (from All-Seeing Interactive) is a great product.  Apple has made strides to incorporate some of the advanced procedures that this allows for, but ASI gives you some great features to easily handle some more complicated operations. It will allow you to easily download multiple files at once and aggregate the progress monitoring of these; It allowed for upload progress monitoring, before apple wired up its NSConnection objects for this purpose and many other features. Having said that I have run across a couple of scenarios and extensions to the basic code that I have found useful.

First I had the need to post multiple files in the same post request up to a web server. The ASI stack has a basic method for attaching an NSData object to your request data. This method simply takes in the data and assigns it “filename” as the default filename for the data that is passed in. Realistically there is no way for ASIFormDataRequest to infer the filename from the binary data. However, this can be quite useful when you are posting binary file data to a web service as the web service is often looking for unique filename identification of each binary part of the posted multipart-form request.

In order to enhance the ASIFormDataRequest to handle this scenario we simply modified the setData: forKey: method to have an override that sets the filename property.

?View Code OBJECTIVE-C
- (void)setData:(NSData *)data forKey:(NSString *)key
{
	[self setData:data forKey:key withFilename:@"file"];
}
 
- (void)setData:(NSData *)data forKey:(NSString *)key withFilename:(NSString *)filename
{
	if (!fileData) {
		fileData = [[NSMutableDictionary alloc] init];
	}
	NSMutableDictionary *file = [[[NSMutableDictionary alloc] init] autorelease];
	[file setObject:data forKey:@"data"];
	[file setObject:filename forKey:@"filename"];
	[fileData setValue:file forKey:key];
	[self setRequestMethod:@"POST"];
}

By adding the overide on the method we have maintained the core functionality and extended it to allow for multiple files with multiple file names.

Tags: , , , , ,

Simple Ruby OAuth with Twitter

We’ve been working on creating a website that would access resources within the cloud, and decided on adopting OAuth as an enabler. The difficulty was in finding a concise and simple example on how to do just that. Everywhere people had fully integrated rails models, controllers and libraries, or complicated explanations with gaps in the code provided.

To fix the problem, we’ve come up with a very simple OAuth example using the Twitter Provider. You’ll need the oauth gem for this to work:

#!/usr/bin/env ruby
 
require 'rubygems'
require 'oauth'
 
consumer = OAuth::Consumer.new "twitter_provided_consumer_key",
                     "twitter_provided_secret_key",
                      { :site => 'http://twitter.com/',
                        :request_token_path => '/oauth/request_token',
                        :access_token_path => '/oauth/access_token',
                        :authorize_path => '/oauth/authorize'}
 
request_token = consumer.get_request_token
puts "Place \"#{request_token.authorize_url}\" in your browser"
print "Enter the number they give you: "
pin = STDIN.readline.chomp
 
access_token = request_token.get_access_token(:oauth_verifier => pin)
 
puts access_token.get('/account/verify_credentials.json')

As you can see, the OAuth gem deals with most of the dirty work, as a matter of fact, almost half of the example file is environment, dependencies and user interaction. There is really only three lines that have any real use.

Tags: , , , ,

Drums ft. Dubya on BeefyBoard, the iPhone SoundBoard

The boys put on a show with the BeefyBoard.

Deploying a Read/Write Resources with your iPhone Application

iPhone development has been a great experience but that does not mean it has not been without its challenges. For instance, if you are planning on saving some sort of dynamic content in your application, you will quickly find that it is not simply a matter of discovering where your application is being stored and writing some files in there.  It is a little bit more involved than that.  In order to help those who come after, we thought it would be worthwhile to share some of our findings.

I think the first thing to realize is that when your application is deployed onto your iPhone/iPod/Simulator you are deploying a complete package. This means that the contents, i.e your code, resources, database and anything else that might be part of your deployment bundle are uniquely signed using your developer’s key which verifies that everything you intended to be in there is there and nothing else. This is a very important concept from a security standpoint and it allows the target device to verify the validity of the bundle to ensure that your application has not been compromised. Now that we have stated that, it’s pretty obvious that it will be impossible to change the deployed project. This doesn’t mean that Apple didn’t think of this inevitability.

The first step to looking into this deployment will be looking at the target deployed file system. The easy way to find this is to log the applications bundle path in the applicationDidFinishLoading method on the appDelegate. You can do this with the following code:

NSLog(@”Application Directory: %@”, NSHomeDirectory());

The output (in the console) for this will show you where you can find the file system. I usually just copy and paste this into a terminal window and jump right to it (just make sure to wrap the directory name in double quotation marks).

If you navigate tot this directory you will see your application (yourapp.app) which is a directory containing all of your code and resources and a Documents and a Library directory. That Documents directory is where our dynamic content will reside.

This seems straightforward enough, just put what we need in the directory and away we go, right? Well not so quick cowboy, there is no idea of an installer that executes when your application downloads. This means we are going to have to figure out how to copy the files into the Documents directory the first time the application loads.

Over here at BeefyApps, we developed this little snippet of code to handle this initial load of data.

-(void)initializeDocumentDirectory
{

NSFileManager *manager = [NSFileManager defaultManager];
NSArray *templateDir = [manager directoryContentsAtPath:[NSString stringWithFormat:@"%@/Documents", [[NSBundle mainBundle] bundlePath]]];

for (id file in templateDir) {

if ([manager fileExistsAtPath:[NSString stringWithFormat:@"%@/Documents/%@", NSHomeDirectory(), file]])

continue;

NSString *srcPath = [NSString stringWithFormat:@"%@/Documents/%@", [[NSBundle mainBundle] bundlePath], file];
NSString *dstPath = [NSString stringWithFormat:@"%@/Documents/%@", NSHomeDirectory(), file];

NSError *error;
[manager copyItemAtPath:srcPath toPath:dstPath error:&error];

}

}

So the code above simply takes a look inside of the bundle, navigated to via the [[NSBundle mainBundle] bundlePath] command. The result of this call will provide you with the string location of the applications executing bundle directory. For the purposes of this code we have a Documents directory which resides inside of the bundle path. We then take the contents of that directory and conditionally copy it file by file into the writable Documents directory.

Pretty simple solution really. I think where this gets interesting is when we start to consider the ramifications for updating your application. I’ll leave that exercise up to the reader at this time though.

Tags: , , , , ,

iPhone 3.0.1 Update Breaks SDK

The title here is a little inflammatory but the solution is quite simple and if we all just read the instructions before installing stuff then maybe we could avoid these problems. But just in case someone else runs across this problem the solution is to run the following command (as indicated by apple in their release notes):

ln -s /Developer/Platforms/iPhoneOS.platform/DeviceSupport/3.0\ \
\(7A341\)/ /Developer/Platforms/iPhoneOS.platform/DeviceSupport/3.0.1

This does assume that you are using the default paths.

Tags: 

Upcoming: New iPhone Game

We at BeefyApps have not been idle since the 1.1 release of our iPhone Soundboard.  A new project has been underway, this time with a little more focus on skill and competitiveness.  Finishing touches are being done on a  recreational pursuit of high scores that can be shown off to your friends.  A little sneak peak of an early alpha build:

Electrophobe for the iPhone
Electrophobe for the iPhone

We are most electrified about the features that you can’t see in the screenshot.

Tags: , , , , ,

BeefyBoard: iPhone Soundboard Ready For iPhone OS 3.0

With the new iPhone OS 3.0 being released today, we here at BeefyApps have been busy updating BeefyBoard: iPhone Soundboard for use with OS 3.0. We’ve found that some of the sound features that worked in OS 2.2.1 were no longer compatible with OS 3.0, specifically recording a sound to the phone and then switching back to playback.

With a little bit of work we’ve fixed these problems and have submitted the new app for approval. The new version should be available to download early next week if the approval process goes smoothly. We’ve also taken the opportunity to add more features to BeefyBoard that will make it the best iPhone Soundboard out there.

What this means to existing users of BeefyBoard and those that purchase it within the next few days is that if you upgrade to OS 3.0, some of the functionality of the app will be broken until you update to the new version of BeefyBoard (when it becomes available on iTunes).

New Features in Version 1.1

  • Added Board Ratings
  • Added Board Comments
  • Added Skin Ratings
  • Added Skin Comments
  • Added Skin Affinity
  • Added Settings Controls
    • Enable/Disable skin affinity
    • Shake sensitivity
  • Added Upload Progress Bar
  • Stop Recording By Pressing Any Button
  • OS 3.0 Compatibility fixes
Tags: , ,

New iPhone Soundboards: Classic Star Trek and Other News

The new Star Trek movie is awesome! (kudos to J.J. Abrams and crew)  But if you are nostalgic for some classic Trek, there are some free Star Trek iPhone soundboards that people have created and uploaded, available to download and play with on the BeefyBoard.  Check them out.

In other news, BeefyApps has been busy working on some Top Secret new projects.  All we can say at the moment is that one of them is free and one of them is a game.  We are all really excited.

Tags: , , , , ,

BAUIStarSlider Source Package

We got some requests from people asking if they can use our BAUIStarSlider code in their applications. We are happy to provide you with the source code and to let you use it in your applications. We only ask that you don’t redistribute it as your own code and if anyone inquires to point them our way. If you do use the star slider code, we would appreciate it if you could give us some credit and link to our site. Other than that feel free to use it however you want and let us know what applications you’ve used it in.

Here is the zip file: BAUIStarSlider Package

Tags: , ,