Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Search | Today's Posts | Mark Forums Read
BOOK: Professional iPhone and iPad Database Application Programming
This is the forum to discuss the Wrox book Professional iPhone and iPad Database Application Programming by Patrick Alessi; ISBN: 978-0-470-63617-6
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional iPhone and iPad Database Application Programming section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old June 13th, 2011, 02:42 PM
Authorized User
 
Join Date: May 2011
Posts: 19
Thanks: 9
Thanked 0 Times in 0 Posts
Default Having a tough time in Ch.7

I'm struggling with errors in chapter 7. I REALLY need to work through this chapter and get everything right. Although I had previously finished the chapter, I decided to restart it from scratch to see if I could resolve the various problems I experienced (being much more careful to add page numbers as comments this time). Still I am having problems (e.g. the most recent error: "Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<NSManagedObject 0x4d1e340> setValue:forUndefinedKey:]: the entity Task is not key value coding-compliant for the key "timeStamp".'").

Before I get too far, I want to resolve something I noticed earlier, namely that my version of XCode creates the code for the RootViewController.h file as follows... (note the underscores & @private)

[BTW, the changes indicated above (e.g. removing underscores & @private) were NOT related to the error quoted above. I have encountered a variety of errors in this chapter & the above referenced changes were related to previous error(s)]


// RootViewController.h

#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>

@interface RootViewController : UITableViewController <NSFetchedResultsControllerDelegate> {

@private
NSFetchedResultsController *fetchedResultsController_;
NSManagedObjectContext *managedObjectContext_;
}

@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain) NSFetchedResultsController *fetchedResultsController;

@end


Since this doesn't match the book (or downloaded code), I tried removing the underscores and @private. This didn't work. My questions...

1-Why does XCode add the underscores and @private? Is this something new? Also, is this something that can be turned off?

2-Was it wrong for me to remove the underscores and @private in order to match the book (and also to match the downloaded version)?

3-If it was okay to remove the underscores & @private, is it possible that some are 'hidden' in a setting somewhere that might also need to be changed? (I am referring here to files other than the .h and .m files that I added to the project)

4-If I should leave the underscores & @private, then I would need to replace every instance of 'fetchedResultsController' and 'managedObjectContext' in the book with 'fetchedResultsController_' (with underscore) and 'managedObjectContext_' (with underscore) respectively, correct? Would any other changes be required?

5-For the recent error (i.e. "the entity Task is not key value coding-compliant for the key "timeStamp"), I don't think it is related to the above, but could anyone give a 'plain English' explanation of this error?

This is a very important chapter for me and I'd really appreciate any help.

Thanks much.
  #2 (permalink)  
Old June 14th, 2011, 09:51 AM
Friend of Wrox
Points: 1,671, Level: 16
Points: 1,671, Level: 16 Points: 1,671, Level: 16 Points: 1,671, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2010
Posts: 298
Thanks: 1
Thanked 62 Times in 60 Posts
Default

The use of @private and underscores is a way to encourage proper encapsulation. It compels (but does not force) you to properly use your accessors. Since many technologies, such as bindings and CoreData rely a lot on KVC and KVO it is a good idea to be as compliant with the standard practice of naming and use of accessors. This simplifies memory management as well. The underscores prevent you from directly setting and getting the ivars. In the following example you can see how to call the variable, you simply use dot syntax and call it from self or use a message send from self.


Code:
@synthesize window=_window;

@synthesize viewController=_viewController;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
     
    self.window.rootViewController = self.viewController; // Uses the accessor - good practice - And necessary if there is any customization in the getter
//  _window.rootViewController = _viewController; // Sets the variable directly - bad practice - And the result will be wrong if there is any customization in the getter
    [self.window makeKeyAndVisible];
    return YES;
}
If you call all variables from self you can totally disregard the underscores, if you try to get them directly (and most of the time you shouldn't), you need to include the underscore as part of the name.

So, leave the underscores in and use the accessors properly. After you redo the project, if point 5 is still a problem we can address it.

Bob
  #3 (permalink)  
Old June 16th, 2011, 09:55 PM
Authorized User
 
Join Date: May 2011
Posts: 19
Thanks: 9
Thanked 0 Times in 0 Posts
Default Still problems after redo...

Thank you for your response. I have spent many hours redoing & checking my work for Chapter 7. As you said, I left the underscores. The app now builds and a good amount of it works - however, I am still experiencing problems. Also, along the way, I encountered a variety of issues...some of which I was able to handle. I think the remaining issues can be grouped into 2 or 3 categories...

FIRST category of problems: The data is apparently getting saved, changed, and deleted okay, but there is a problem updating the main tasks list. For example, if I add a new task using the '+' button, it doesn't appear until I build again. Same thing for deletes and changes (deleted tasks still appear until after a new build, changed tasks appear unchanged until a new build...unless you click on the detail screen). I spent a lot of time on this and I can't figure it out. This is the BIGGEST area of concern. I REALLY need help with this issue.

SECOND category of problems concerns dates. For example: The app won't allow me to select the current date. Also, tasks with a due date of today may show up in red (even though red should be reserved for dates prior to the current date). I could probably make manual adjustments for these issues, but it seems to me things should work the way the book says...unless there's an error/omission in the book? (Maybe dealing with the time?)

THIRD category of problems: Crashes. Sometimes the app crashes when the buttons on the bottom are pressed (but other times the buttons work fine). I suspect these errors really have to do with the 'first category of problems' refereed to above. I think when changes to a record are 'pending' (e.g. completed but for some reason they don't display), it causes the buttons to malfunction. Therefore, I am pretty sure this problem will be resolved when the first issue above is resolved.

I've spent a lot of time trying to track down/fix the above, but I'm really stuck at this point. One thing I did was to add NSLog statements to every method/function in my project. I have this version available if it might help someone determine what the problem is. I know I can't post it here, but maybe it could be sent via e-mail if someone would be willing to review it.

Also, so you'll know - and also for the benefit of others who may be struggling with this chapter - the following are some notes concerning issues I encountered in Chapter 7 (may not be in order). Please keep in mind that I am not sure that the various 'fixes' I tried were correct. (I would appreciate a heads up if they were, in fact, incorrect.) I apologize in advance if there are any typos...these were taken from some quick notes I made.

* As mentioned previously, the default code generated by XCode included some underscores, such as NSFetchedResultsController *fetchedResultsController_; and NSManagedObjectContext *managedObjectContext_;. As per your earlier reply, I left those alone rather than matching the book. I wasn't sure if I was supposed to add underscores to the code in the book (I tried adding some, but got errors, so I removed them)

* On pg. 171, the app was supposed to build without error. However, I got the same error four times, namely "Request for member 'tableView' in something not a structure or union'". Based on page 169 (2nd to last paragraph, not including the code), the fix seems to be to change from 'tableview' to 'taskTableView'. This change resolved the error in all four places.

* I was forced to add an underscore here: RootViewController.m/cellForRowAtIndexPath / Task *managedTaskObject = [fetchedResultsController_ objectAtIndexPath:indexPath] ... Without the underscore I got the error 'fetchedResultsController' undeclared

* XCode automatically generated the code for @property isOverdue / @dynamic isOverdue. If you put it in again (e.g. from the book), you'll get an error

* The file extension in my project is ".datamodeled" vs. "datamodel'" in the downloaded version. I researched this online and don't think it would be an issue, but I thought I'd mention it just in case it is important

* On page 179, the book says we should be able to select tasks and view them, but the code for RootViewController.m didSelectRowAtIndexPath was missing. I got the code from the downloaded version & inserting the downloaded code resolved the problem

* Once again, I got the timeStamp error ("the entity Task is not key value coding-compliant for the key "timeStamp""). Commenting out two lines (NSManagedObject *managedObject = [self.fetchedResultsController objectAtIndexPath:indexPath]; AND cell.textLabel.text = [[managedObject valueForKey:@"timeStamp"] description];) eliminated the error

* On pg. 203 - there is an extra semi-colon that should be removed

* I added [NSFetchedResultsController deleteCacheWithName:nil]; to RootViewController.m - toolbarFilterHiPri AND RootViewController.m -toolbarFilterAll as per another post on this forum

* There are differences in the code automatically generated by XCode and the book in the location .h and location.m file. I tried making the items above match, but it didn't resolve the problems I'm experiencing, so I rolled the changes back. (There are also other places where the code differs from the book)

* XCode seems to have put @interface... at the top of the RootViewController.m (M) file. I'm not sure why, but I don't think this would affect the problems I'm having

* XCode also added some import statements by default that I don't recall seeing in the downloaded version (e.g. in RootViewController.h and TasksAppDelegate.h - #import <UIKit/UIKit.h> and #import <CoreData/CoreData.h>). Again, I don't think this is relevant to my errors, but I thought I'd mention it

A huge thank you in advance for any help.
  #4 (permalink)  
Old June 17th, 2011, 08:14 AM
Friend of Wrox
Points: 1,671, Level: 16
Points: 1,671, Level: 16 Points: 1,671, Level: 16 Points: 1,671, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2010
Posts: 298
Thanks: 1
Thanked 62 Times in 60 Posts
Default

If you want to send your project to
ipadhelper@me.com
I can try to trace down your problems. Unfortunately, I don't actually own the book, so I can't go through the project step by step as outlined in the book and point out differences that come about through the use of newer versions of Xcode and the SDK. I have been able to provide some answers in other threads for this book through analyzing the downloadable source code and making suggestions based on that. That is how I would approach your issues. Though I may go through the book at some point it is not on my near term agenda.
So feel free to send the project. I would be able to take some time, possibly today and definitely tomorrow to look at it.

Bob
The Following User Says Thank You to thepianoguy For This Useful Post:
tecky10 (June 17th, 2011)
  #5 (permalink)  
Old June 17th, 2011, 12:25 PM
Authorized User
 
Join Date: May 2011
Posts: 19
Thanks: 9
Thanked 0 Times in 0 Posts
Default Thanks!

Thanks so much! I have sent the project to you via e-mail. It's 1.1 MB (zipped). Please let me know if you do not receive the file.

Thanks again.
  #6 (permalink)  
Old July 15th, 2011, 10:39 AM
Registered User
 
Join Date: Jun 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Chapter 7 Results

I have run into a similar problem. Everything else works except the main tableView never shows anything.

It has something to do with the tableView delegate methods not getting called. I noticed that RootViewController is not a UITableViewDelegate. I think I am missing something about how the fetched results interacts with the table view. I re-read the book several times, but I don't see where the delegate is set up.

Any suggestions?
  #7 (permalink)  
Old July 15th, 2011, 12:40 PM
Friend of Wrox
Points: 1,671, Level: 16
Points: 1,671, Level: 16 Points: 1,671, Level: 16 Points: 1,671, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2010
Posts: 298
Thanks: 1
Thanked 62 Times in 60 Posts
Default

If you want I can look at your project and work out the problem if you send it to
ipadhelper@me.com

Bob
  #8 (permalink)  
Old July 15th, 2011, 12:51 PM
Authorized User
 
Join Date: May 2011
Posts: 19
Thanks: 9
Thanked 0 Times in 0 Posts
Default Ch. 7 problems

The problem you describe does not sound like one I had. In my case, the change that made the app finally work properly was replacing reloadData with endUpdates. I looked at my notes and I see that I also had to replace two instances of "fetchedResultsController_" with "self.fetchedResultsController", but even without this last change, the app worked at that point.

In case it helps, the way I have been most successful at narrowing down problems in my code with respect to the book is by the following method...

1-Create a copy of the problem project
2-Download a copy of the functioning project from the Wrox site
3-Replace the text (i.e. copy/paste) in the problem project file from the downloaded version, one file at a time. Start with the file you suspect has the problem. For example, if you think your problem is most likely to be in the RootViewController.m file, start there. (It doesn't really matter if you are wrong about which file, this just saves you some time by starting with the file you think is the most likely culprit)
4-After you've replaced the text in the problem project's file with the text from the functioning project's file, try building. If it builds successfully, you have narrowed your problem down to that file. If it doesn't build successfully, go on to the next file. Keep going one file at a time until your project builds.
5-Once you've narrowed down the problem file, review your version against the downloaded version. For tricky ones, I have used .doc compare (from Microsoft Word). This can help narrow down typos that are especially difficult to notice manually (e.g. an "f" instead of an "r")

I find the above usually works best for me. You could also put NSLog statements in both the downloaded version and your version to verify that they both call the same methods.

I'm sorry I couldn't help more specifically for your problem, but hopefully something above might be of some use.

Best wishes.
  #9 (permalink)  
Old July 18th, 2011, 10:38 PM
Registered User
 
Join Date: Jun 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

My DataSource connection was either never connected or got deleted somehow. It is now working.

I also had to add a [NSFetchedResultsController deleteCacheWithName:nil]; in the HiPri and All functions otherwise I got a sigAbort.
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Ch 9-but will learn more abt User Controls (ch 8) btcomp BOOK: Beginning ASP.NET 4 : in C# and VB 1 August 24th, 2010 11:40 AM
Fatal Error - A tough one! gargamel BOOK: Beginning PHP5, Apache, and MySQL Web Development ISBN: 978-0-7645-7966-0 3 April 4th, 2007 04:25 PM
tough code please help... xanderxvr Classic ASP Basics 2 January 27th, 2005 12:23 PM
Tough one !! xls as frontend with mdb as backend Stanny Access 5 January 7th, 2005 11:01 AM



All times are GMT -4. The time now is 02:29 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.