CoreData getting started tutorial

Posted: November 26, 2010 in iPhone
Tags: ,

CoreData frame work looks very complicated at first sight, but it will be easy once you get used to it. This framework is made to work with sqlite data and UITableView, as you may know already. It changes our perception of database query into a more familiar “class object”.

Alright, let’s get started. A good project starting point can be found here with a sample project for download.
http://www.bukisa.com/articles/180100_iphone-core-data-tutorial-part-1

Don’t worry, there is a purpose for this blog post :P. However, the above tutorial is missing the process of creating a new Entity and work with it.

First off, create a new Entity in the project’s default .xcdatamodel file. Go ahead and modify the fetchedResultsController in the RootViewController to point to the correct identity (no need to mess with the ProjectNameAppDelegate files), then “delete” the .sqlite file in the Library/Application Support/iPhone Simulator/4.1/Applications/16C74C24-A169-449E-9463-7907032EA853/Documents and rebuild the project to make it work. (Or tap and hold on the App icon int he simulator and delete it).

Not so sure if this happens with all machines or not, but it happens to me T_T

Using predicate

Create a copy of the method “- (NSFetchedResultsController *)fetchedResultsController”, name it something different, like “fetchedResultsControllerModified”, and then add the predicate code inside. I put it right after “[fetchRequest setEntity:entity];”. Code here are based on the default Navigation based project with CoreData ticked.

NSPredicate *predicate = [NSPredicate predicateWithFormat:@”name = %@”, @”sample 3″];
//NSPredicate *predicate = [NSPredicate predicateWithFormat:@”number < 50″];
[fetchRequest setPredicate:predicate];

Don’t forget to

  • In .h file, declare “NSFetchedResultsController *fetchedResultsControllerModified_;”
  • In @synthesize add “fetchedResultsControllerModified=fetchedResultsControllerModified_;”
  • Perform the fetch operation in – (void)viewDidLoad

NSError *error_mod = nil;
if (![[self fetchedResultsControllerModified] performFetch:&error_mod]) {
NSLog(@”Unresolved error %@, %@”, error_mod, [error userInfo]);
abort();
}

  • And of course, “[fetchedResultsControllerModified_ release];” in dealloc.

You can access the fetch result by doing this (getting only the first result)…

Record *record = nil;

if ([[self.fetchedResultsControllerModified fetchedObjects]count] > 0) {

record = (Record *)[fetchedResultsControllerModified_ objectAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];

}

else {

// handle no data found

}

Advertisements

Few days ago i end up having a very anoying problem, every time i tried to sync an adHoc application on iTunes i was having this message :

“The application X was not installed on the iPhone Y because its resources have been modified”

And the thing is that this problem only happens on PC!

I didn’t really find out why i was having this error and how i could solve it, but i did find how to avoid it. Instead of delivering the traditionnal .app file we’ll create an .ipa file.

To create this IPA file it’s very easy :

  • Create a folder named Payload
  • Put your .app file in this folder
  • Then compress the Payload folder in Zip
  • Finally change extension .zip to .ipa

And that’s it, send your .ipa file to your Client/Beta testers with the corresponding provisionning profile.

Credit: http://www.iphonedevsample.com/2010/03/15/solution-for-itunes-error-resources-have-been-modified/

How to make indexPaths like UITableView?

Posted: November 25, 2010 in iPhone
Tags: ,

I was doing CoreData and tried to get a specific fetch result without going through the table index path.
Basically, you can use
[NSIndexPath indexPathForRow:i inSection:j] for the index path.
Credit: http://urinieto.com/2009/05/how-to-get-allindexpaths-from-a-uitableview/


I find this a very basic question, but somewhat difficult to find in the Apple documentation. The answer is not found in the NSIndexPath Class Reference, but in the NSIndexPath UIKit Additions. That’s why I think some people would not know about the following method which allows us to get a specified indexPath from a UITableView.

The method is:

+ (NSIndexPath *)indexPathForRow:(NSUInteger)row inSection:(NSUInteger)section

And it returns an indexPath object initialized with the indexes of a specific row and section in a table view.

With this method we will be able to retrieve all the indexPaths from a table view performing a loop like this:

NSInteger nSections = [tableView numberOfSections]; for (int j=0; j<nSections; j++) {   NSInteger nRows = [tableView numberOfRowsInSection:j];   for (int i=0; i<nRows; i++) {     NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:j];     //Do something with your indexPath. Maybe you want to get your cell, // like this:     //UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];   } } 

I hope this helps. If you find an easier way, please tell me.

I was trying to put this in google extension dev zone, but looks like they want $5. So I zipped it and post it here instead. Download extension here

This extension allows you to access blocked sites such as facebook by changing protocol to https! My office had a firewall block on facebook, youtube, etc. but I can sneak out using https. However, unlike Firefox, Chrome does not have an extension to automatically add https to the url. So I spend my freetime on this and get to know how Chrome extension works more or less.

My own custom CallOutView

Posted: November 5, 2010 in iPhone
Tags:

A few days ago I went through searches and look for how to create a comment text balloons (like comics) in my project. I came across an Apple’s mysterious “undocumented” class named CallOutView. I had to manually add headers and It looks just like in the standard MapKit.

Read the rest of this entry »

How to add Cocos2D on UIView!

Posted: November 5, 2010 in iPhone
Tags:

As far as I have searched, I found a nice blog explaining how to add Cocos2D library into a UIKit View based application. But it is still missing how to actually add the sprites and other Cocos2D elements into the application. If will try to continue doing it myself.

Stay tuned 😀

PS. First Post!