#14 ✓resolved
Isaac Kearse

No results found when accessing json from Rails 3 app

Reported by Isaac Kearse | March 26th, 2010 @ 04:44 AM

Hi Mike,

My iPhone app is making a json call out to my rails app successfully and rendering a CoreTableController but it is just displaying the message "No results found".

Here are the logs from the Debugger Console:

[CoreManager#enqueueRequest] request queued: http://localhost:3000/events.json Find Event [#1] in context <NSManagedObjectContext: 0x3b202a0> (found 0)
*** -[NSManagedObject update:withOptions:]: unrecognized selector sent to instance 0x3d100c0

The rails log confirms that the request is made successfully, but nothing shows up.
Any idea what might be going on here?

Cheers,
Isaac

Comments and changes to this ticket

  • mklaurence (at gmail)

    mklaurence (at gmail) March 26th, 2010 @ 05:59 AM

    It looks like maybe the object that's created is of class NSManagedObject and not your CoreResource subclass (NSManagedObject doesn't have the method update:withOptions:, but CoreResource does).

    Are you defining the entity's class as "Event" in your xcdatamodel file?

    You could put an NSLog at line 311 of CoreResource.m to check what kind of object it is before the update:withOptions: happens:

    // Insert new managed object into context
    CoreResource *resource = [[self alloc] initWithEntity:[self entityDescription] 
        insertIntoManagedObjectContext:context];
    
    NSLog(@"Created resource: %@", resource);
    
    // Update new object with properties
    [resource update:parameters withOptions:options];
    
  • Isaac Kearse

    Isaac Kearse March 26th, 2010 @ 06:30 AM

    Thanks Mike - I changed the class to Event in the xcdatamodel file, and I'm no longer getting the "unrecognized selector" error...but I'm still getting the "No results found." message.

    The log starts off the same way:

    [CoreManager#enqueueRequest] request queued: http://localhost:3000/events.json Find Event [#1] in context <NSManagedObjectContext: 0x3d1e310> (found 0)

    Then it seems to set all the fields, ending with resourceId:
    [Event] Setting remote field: id, local field: resourceId, value: 1

    And there don't appear to be any errors but nothing shows up. Any ideas?

    Cheers,
    Isaac

  • mklaurence (at gmail)

    mklaurence (at gmail) March 26th, 2010 @ 06:41 AM

    Are you performing a fetch in the controller (in viewWillAppear or somewhere)?

    [[self coreResultsController] fetchWithSort:@"created_at DESC"];
    
    • If you exit the app and come back in (with a saved local object), does it still say "no results found"?
    • Is your controller called EventsController / or have you overridden the -(Class)model method?

    Can you post any of your controller code?

    ~Mike

  • Isaac Kearse

    Isaac Kearse March 26th, 2010 @ 06:50 AM

    Yes if I exit and come back in it still says No results found. Its called Events Controller, and I haven't overidden -(Class)model.

    Controller Code:

    #import "EventsController.h"
    #import "Event.h"
    
    @implementation EventsController
    
    - (void) loadView { [self loadTableView]; }
    
    - (void) viewWillAppear:(BOOL)animated {
      [Event findAllRemote];
      [[self coreResultsController] fetchWithSort:@"created_at DESC"];
    }
    
    @end
    

    Called from the app delegate here:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
      [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackOpaque];
      self.coreManager = [[[CoreManager alloc] init] autorelease];
      coreManager.logLevel = 100;
      coreManager.remoteSiteURL = @"http://localhost:3000";
      window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
      eventsController = [[EventsController alloc] init];
      navigationController = [[UINavigationController alloc] initWithRootViewController:eventsController];
      [window addSubview:[navigationController view]];
      [window makeKeyAndVisible];
        return YES;
    }
    

    The problem seems to be happening in the "update:withOptions:" method - I put a logging message before and after that method and it never returns.

  • mklaurence (at gmail)

    mklaurence (at gmail) March 26th, 2010 @ 06:56 AM

    Hmmm... yes, I've noticed before that update:withOptions fails silently sometimes. I'll have to investigate a way to make it more vocal.

    For now, can you try putting a breakpoint right after the NSLog that outputs "Setting remote field" (line #504 of CoreResource) ? It must be failing shortly thereafter.

  • Isaac Kearse

    Isaac Kearse March 26th, 2010 @ 07:00 AM

    ok narrowed it down to line 518:

    [self setValue:value forKey:localField];
    

    It doesn't return from that method when setting resourceId

  • Isaac Kearse

    Isaac Kearse March 26th, 2010 @ 07:06 AM

    Don't know if this is anything to do with it but when I originally created the model I forgot to include the resourceId field. I added it later to the model and to the Event class as a dynamic property. I also reset the simulator content and settings and Cleaned all targets in Xcode. But maybe there is something else I need to do to make sure its using the new data model?

  • mklaurence (at gmail)

    mklaurence (at gmail) March 26th, 2010 @ 07:13 AM

    All you have to do is delete the app from the simulator, and the next time you install the app it will create a new database with the new data model.

    Do you have

    @property (nonatomic, retain) NSNumber* resourceId;  // If it's a number..
    
    in your Event.h?

    Also make sure that the value it's trying to set (1, in your case) is of the same class type as the property. E.g., if it's coming in as a string, make sure resourceId is a string (another thing that would be nice is automatic conversion in that case...)

  • Isaac Kearse

    Isaac Kearse March 26th, 2010 @ 07:25 AM

    AWESOME!!! IT WORKS!

    I changed it to an NSNumber in the Event class, and in the model I changed it to an "Integer 16"

    Thanks a lot for all your help.

    Now I'm going to try and create a "show" view & an EventController.
    ( Hoping this will be just a few lines of code too - but maybe thats a bit too hopeful :)

    Cheers,
    Isaac

  • mklaurence (at gmail)

    mklaurence (at gmail) March 26th, 2010 @ 07:31 AM

    • State changed from “new” to “resolved”

    Yay!

    I think some auto-conversion for that sort of thing should be easy enough... although it would be good, too, if it logged some complaints.

    Thanks for digging in to the project - more stuff coming soon!

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

Local & remote resource management library for Objective-C using Core Data with Rails-like syntax. WARNING: The API is currently under heavy development and will undoubtedly change.

Shared Ticket Bins

People watching this ticket

Pages