Skip to content

Instantly share code, notes, and snippets.

@hamin
Created August 17, 2012 09:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hamin/3377350 to your computer and use it in GitHub Desktop.
Save hamin/3377350 to your computer and use it in GitHub Desktop.
Create custom classes by custom classes inheriting from NSFNanoObject
#import "NSFNanoObject.h"
@interface Person : NSFNanoObject
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSString *location;
@end
#import "Person.h"
#import "NanoStore.h"
@implementation Person
@synthesize name;
@synthesize location;
- (id)initFromDictionaryRepresentation:(NSDictionary *)theDictionary
{
[self clearNulls:theDictionary];
self = [super initFromDictionaryRepresentation:theDictionary];
if (self) {
self.name = [theDictionary objectForKey:@"name"];
self.location = [theDictionary objectForKey:@"location"];
}
return self;
}
@end
#import "Person.h"
@interface SomeController ()
@end
@implementation SomeController
-(void)awakeFromNib
{
// ...
NSString *thePath = @"~/Desktop/peopleApp.database";
NSFNanoStore *nanoStore = [NSFNanoStore createAndOpenStoreWithType:NSFPersistentStoreType path:thePath error:nil];
// Obtain the engine
NSFNanoEngine *nanoStoreEngine = [nanoStore nanoStoreEngine];
// Set the synchronous mode setting
[nanoStoreEngine setSynchronousMode:SynchronousModeOff];
[nanoStoreEngine setEncodingType:NSFEncodingUTF16];
// Open the document store
[nanoStore openWithError:nil];
// This should initialize a newly created NanoObject for Person as the NanoObject.h suggest
// + (NSFNanoObject*)nanoObjectWithDictionary:(NSDictionary *)theDictionary;
// /** * Initializes a newly allocated NanoObject with the given dictionary.
Person *aPerson = [[Person alloc] initFromDictionaryRepresentation:personDict];
// ...
}
@end
@tciuro
Copy link

tciuro commented Aug 17, 2012

Hamin,

A couple things:

  1. The documentation states that any object that's going to be stored in NanoStore must conform to NSFNanoObjectProtocol. That can be done by adopting and implementing the protocol (e.g. @interface Person : NSObject <NSFNanoObjectProtocol, NSCopying>) or by subclassing NSFNanoObject (e.g. @interface Person : NSFNanoObject).

  2. You don't have to call initFromDictionaryRepresentation in Person directly. You set your properties directly: myPerson.name = @"John Doe"; myPerson.location = @"United States"; That's it. NanoStore will use - (NSDictionary *) nanoObjectDictionaryRepresentation (that the class Person provides) and implement also implement

  • (id) initNanoObjectFromDictionaryRepresentation:(NSDictionary *) theDictionary forKey:(NSString *) aKey store:(NSFNanoStore *) theStore; In the implementation, just retrieve and set your properties in Person and don't forget to call super.

@pboling
Copy link

pboling commented Aug 17, 2012

What language is this?

@hamin
Copy link
Author

hamin commented Aug 17, 2012

@tciuro I was trying to do is actually subclass NSFNanoObject. But what I really wanted to do is create a new instance of Person (which would be a NSFNanoObject) by providing it a dictionary. I was hoping that when i do that, in my classes init method (i thought the proper method to use was initFromDictionaryRepresentation, but I guess I'm wrong) and then have the initializer set the proper properties for the instance.

Basically I would rather create myPerson with a dictionary in the init method and have it persist to a NSFNanoObject than to set each of the properties indvidually after the instance has been instantiated. Can this be done? I'm a missing something? I hope you see what I"m trying to do.

@pboling its Objective-C

@tciuro
Copy link

tciuro commented Aug 17, 2012

Yes, I now understand what you mean. I have realized that NSFNanoObject does not initialize the object properly if it's inited like you want to. I just didn't think someone would initialize it like this (though it makes perfect sense.) So this is what I'm going to fix issue #34.

Advice: do not use the properties directly. In the init method, access them directly since the object may not be fully ready when you invoke them via property. To do that, replace the following code:

@synthesize name = _name;
@synthesize location = _location;

  • (id)initFromDictionaryRepresentation:(NSDictionary *)theDictionary
    {
    [self clearNulls:theDictionary];

    if (self = [super init]) {
    name = [[theDictionary objectForKey:@"name"]copy];
    location = [[theDictionary objectForKey:@"location"]copy];
    }

    return self;
    }

I'll be checking the fix very soon.

@hamin
Copy link
Author

hamin commented Aug 18, 2012

@tciuro GREAT! I will be checking for the fix and upgrade NanoStore in my project soon. So far its been a pleasure to work with :). And thanks for the tip for no using properties directly. Still fairly new to Cocoa and Obj-C but so far really enjoying it. Thanks again :)

@hamin
Copy link
Author

hamin commented Aug 28, 2012

@tciuro it works!!! Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment