Created
October 24, 2012 11:38
-
-
Save blazovics/3945611 to your computer and use it in GitHub Desktop.
bme-ios - iTravel Core Data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
+ (UIImage*)imageAtUrl:(NSString *)path{ | |
NSData* data = [[NSFileManager defaultManager] contentsAtPath:path]; | |
return [UIImage imageWithData:data]; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
+ (NSString*)saveImage:(UIImage*)image{ | |
NSString *uuid = [[NSProcessInfo processInfo] globallyUniqueString]; | |
NSString* fileName = [NSString stringWithFormat:@"%@.png",uuid]; | |
NSData* imageData = UIImagePNGRepresentation(image); | |
NSURL* documentsURL = [ITAppDelegate applicationDocumentsDirectory]; | |
NSURL* imageURL = [documentsURL URLByAppendingPathComponent:fileName]; | |
NSString* filePath = [imageURL relativePath]; | |
[[NSFileManager defaultManager] createFileAtPath:filePath contents:imageData attributes:nil]; | |
return filePath; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)addTrip:(NSDictionary*)tripDict{ | |
NSManagedObjectContext* moc = [self managedObjectContext]; | |
Trip* newTrip = [NSEntityDescription insertNewObjectForEntityForName:@"Trip" inManagedObjectContext:moc]; | |
newTrip.name = [tripDict valueForKey:@"name"]; | |
newTrip.location = [tripDict valueForKey:@"location"]; | |
newTrip.descriptionInfo = [tripDict valueForKey:@"description"]; | |
newTrip.image = [tripDict valueForKey:@"image"]; | |
[self saveContext]; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (NSManagedObjectContext *)managedObjectContext | |
{ | |
if (__managedObjectContext != nil) | |
{ | |
return __managedObjectContext; | |
} | |
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator]; | |
if (coordinator != nil) | |
{ | |
__managedObjectContext = [[NSManagedObjectContext alloc] init]; | |
[__managedObjectContext setPersistentStoreCoordinator:coordinator]; | |
} | |
return __managedObjectContext; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (NSManagedObjectModel *)managedObjectModel | |
{ | |
if (__managedObjectModel != nil) | |
{ | |
return __managedObjectModel; | |
} | |
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"ITTravelModel" withExtension:@"momd"]; | |
__managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL]; | |
return __managedObjectModel; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator | |
{ | |
if (__persistentStoreCoordinator != nil) | |
{ | |
return __persistentStoreCoordinator; | |
} | |
NSURL *storeURL = [[ITAppDelegate applicationDocumentsDirectory] URLByAppendingPathComponent:@"itravel.sqlite"]; | |
NSError *error = nil; | |
//Remove the whole database at first call | |
//[[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil]; | |
__persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]]; | |
if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) | |
{ | |
NSLog(@"Unresolved error %@, %@", error, [error userInfo]); | |
abort(); | |
} | |
return __persistentStoreCoordinator; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)saveContext | |
{ | |
NSError *error = nil; | |
NSManagedObjectContext *managedObjectContext = self.managedObjectContext; | |
if (managedObjectContext != nil) | |
{ | |
if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) | |
{ | |
NSLog(@"Unresolved error %@, %@", error, [error userInfo]); | |
abort(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment