Skip to content

Instantly share code, notes, and snippets.

@abbood
Created February 27, 2014 14:09
Show Gist options
  • Save abbood/9250764 to your computer and use it in GitHub Desktop.
Save abbood/9250764 to your computer and use it in GitHub Desktop.
the way i create restkit model
//
// Model.m
// TNN
//
// Created by Abdullah Bakhach on 11/20/13.
// Copyright (c) 2013 larochesoft. All rights reserved.
//
#import <CoreData/CoreData.h>
#import "RKManagedObjectStore.h"
#import "Model.h"
#import "TNNRestClient.h"
#import "RKXMLReaderSerialization.h"
static Model *singleton = nil;
@implementation Model
+ (id)getSingleton {
@synchronized(self) {
if (singleton == nil) {
singleton = [[self alloc] init];
[singleton setupDatabase];
}
}
return singleton;
}
- (void)setupDatabase {
NSError *error = nil;
NSURL *modelURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"TNN" ofType:@"momd"]];
// NOTE: Due to an iOS 5 bug, the managed object model returned is immutable.
NSManagedObjectModel *managedObjectModel = [[[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL] mutableCopy];
singleton.managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];
// Initialize the Core Data stack
[singleton.managedObjectStore createPersistentStoreCoordinator];
// Configure Core Data Integration
BOOL success = RKEnsureDirectoryExistsAtPath(RKApplicationDataDirectory(), &error);
if (! success) {
RKLogError(@"Failed to create Application Data Directory at path '%@': %@", RKApplicationDataDirectory(), error);
}
//NSDictionary *options = @{ NSSQLitePragmasOption : @{@"journal_mode" : @"DELETE"} };
NSString *path = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"Store.sqlite"];
NSPersistentStore *persistentStore = [singleton.managedObjectStore addSQLitePersistentStoreAtPath:path
fromSeedDatabaseAtPath:nil
withConfiguration:nil
options:nil
error:&error];
if (! persistentStore) {
RKLogError(@"Failed adding persistent store at path '%@': %@", path, error);
}
[singleton.managedObjectStore createManagedObjectContexts];
// Set the default store shared instance
[RKManagedObjectStore setDefaultStore:singleton.managedObjectStore];
// Configure the object manager
TNNRestClient *client = [TNNRestClient getSingleton];
singleton.objectManager = [[RKObjectManager alloc] initWithHTTPClient:client];
singleton.objectManager.managedObjectStore = singleton.managedObjectStore;
[RKMIMETypeSerialization registerClass:[RKXMLReaderSerialization class] forMIMEType:@"text/xml"];
[[RKObjectManager sharedManager] setAcceptHeaderWithMIMEType:RKMIMETypeTextXML];
[RKObjectManager setSharedManager:singleton.objectManager];
// configure date fomratting
NSDateFormatter *dateFormatter = [NSDateFormatter new];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
dateFormatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
[RKObjectMapping addDefaultDateFormatter:dateFormatter];
}
- (void)setResponseDescriptors {
//[[Exam getSingleton] addResponseDescriptor];
//[[Subject getSingleton] addResponseDescriptor];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment