Skip to content

Instantly share code, notes, and snippets.

@bradfordcp
Created August 3, 2011 01:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bradfordcp/1121689 to your computer and use it in GitHub Desktop.
Save bradfordcp/1121689 to your computer and use it in GitHub Desktop.
RemoteEntity Example - Assumes the AppDelegate has a connection to the ManagedObjectContext.
//
// RemoteEntity.h
//
// Created by Christopher Bradford on 5/6/10.
// Copyright 2010 INM United. All rights reserved.
//
@interface RemoteEntity : NSManagedObject
{
}
@property (nonatomic, retain) NSNumber * remote_update;
@property (nonatomic, retain) NSNumber * remote_id;
// Helper method for retrieving an instance of a remote entity given its id
+ (RemoteEntity *)entityWithRemoteID:(NSNumber *)remote_id;
// Helper method for retrieving the EntityDescription of the entity
+ (NSEntityDescription *)entityDescription;
@end
//
// RemoteEntity.m
//
// Created by Christopher Bradford on 5/6/10.
// Copyright 2010 INM United. All rights reserved.
//
#import "RemoteEntity.h"
@implementation RemoteEntity
@dynamic remote_update;
@dynamic remote_id;
#pragma mark Core Data Helper Methods
+ (RemoteEntity *)entityWithRemoteID:(NSNumber *)remote_id
{
NSFetchRequest *req = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:NSStringFromClass([self class]) inManagedObjectContext:APP_DELEGATE.managedObjectContext];
[req setEntity:entity];
[req setPredicate:[NSPredicate predicateWithFormat:@"remote_id = %@", remote_id]];
NSError *error;
NSArray *entities = [APP_DELEGATE.managedObjectContext executeFetchRequest:req error:&error];
[req release];
if ([entities count] != 0) {
return [entities objectAtIndex:0];
}
return nil;
}
+ (NSEntityDescription *)entityDescription
{
return [NSEntityDescription entityForName:NSStringFromClass([self class]) inManagedObjectContext:APP_DELEGATE.managedObjectContext];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment