Last active
December 15, 2015 10:29
-
-
Save imrekel/5245758 to your computer and use it in GitHub Desktop.
bme-ios - UberNotebook
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
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; | |
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Note" | |
inManagedObjectContext:context]; | |
[fetchRequest setEntity:entity]; | |
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error]; | |
for (NSManagedObject *note in fetchedObjects) | |
{ | |
NSLog(@"Title: %@", [note valueForKey:@"title"]); | |
NSLog(@"Content: %@", [note valueForKey:@"content"]); | |
} |
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* context = [self managedObjectContext]; | |
NSManagedObject* notebook = [NSEntityDescription insertNewObjectForEntityForName:@"Notebook" | |
inManagedObjectContext:context]; | |
NSString* notebookTitle = [NSString stringWithFormat:@"Jegyzetek %d", arc4random_uniform(1000)]; | |
[notebook setValue:notebookTitle forKey:@"title"]; | |
NSManagedObject* note = [NSEntityDescription insertNewObjectForEntityForName:@"Note" inManagedObjectContext:context]; | |
[note setValue:@"Személyes adatok" forKey:@"title"]; | |
[note setValue:@"Név: Almás Aladár" forKey:@"content"]; | |
// [NSDate date] az aktuális idővel lesz inicializáva | |
[note setValue:[NSDate date] forKey:@"creationDate"]; | |
[note setValue:notebook forKey:@"notebook"]; | |
NSError *error; | |
if (![context save:&error]) | |
{ | |
NSLog(@"Sikertelen mentés: %@", [error localizedDescription]); | |
} | |
else | |
{ | |
NSLog(@"Sikeres mentés"); | |
} |
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)fetchNotebooks | |
{ | |
NSManagedObjectContext* context = [[UNAppDelegate sharedAppDelegate] managedObjectContext]; | |
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; | |
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Notebook" inManagedObjectContext:context]; | |
[fetchRequest setEntity:entity]; | |
NSError *error; | |
_notebooks = [context executeFetchRequest:fetchRequest error:&error]; | |
} |
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)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex | |
{ | |
if (buttonIndex == 1) | |
{ | |
NSString* content = [[alertView textFieldAtIndex:0] text]; | |
NSManagedObjectContext* context = [[UNAppDelegate sharedAppDelegate] managedObjectContext]; | |
NSEntityDescription* noteEntity = [NSEntityDescription entityForName:@"Note" inManagedObjectContext:context]; | |
Note* note = (Note *)[[NSManagedObject alloc] initWithEntity:noteEntity insertIntoManagedObjectContext:context]; | |
note.content = content; | |
if ([content length] < 30) | |
note.title = content; | |
else | |
note.title = [content substringToIndex:30]; | |
note.creationDate = [NSDate date]; | |
[self.notebook addNotesObject:note]; | |
} | |
} |
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)controllerWillChangeContent:(NSFetchedResultsController *)controller | |
{ | |
[self.tableView beginUpdates]; | |
} | |
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath | |
{ | |
UITableView *tableView = self.tableView; | |
switch(type) { | |
case NSFetchedResultsChangeInsert: | |
[tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; | |
break; | |
case NSFetchedResultsChangeDelete: | |
break; | |
case NSFetchedResultsChangeUpdate: | |
break; | |
case NSFetchedResultsChangeMove: | |
break; | |
} | |
} | |
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller | |
{ | |
[self.tableView endUpdates]; | |
} |
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)viewDidLoad | |
{ | |
[super viewDidLoad]; | |
self.title = self.notebook.title; | |
NSManagedObjectContext* context = [[UNAppDelegate sharedAppDelegate] managedObjectContext]; | |
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; | |
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Note" | |
inManagedObjectContext:context]; | |
[fetchRequest setEntity:entity]; | |
// szűrés a kiválasztott Notebook-ra | |
NSPredicate* predicate = | |
[NSPredicate predicateWithFormat:@"notebook == %@", _notebook]; | |
[fetchRequest setPredicate: predicate]; | |
// rendezés creationDate szerint | |
NSSortDescriptor *sort = [[NSSortDescriptor alloc] | |
initWithKey:@"creationDate" ascending:NO]; | |
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]]; | |
// egyszerre maximum 30 note lekérése | |
[fetchRequest setFetchBatchSize:30]; | |
_fetchedResultsController = | |
[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest | |
managedObjectContext:context sectionNameKeyPath:nil | |
cacheName:nil]; | |
NSError *error; | |
if (![_fetchedResultsController performFetch:&error]) | |
{ | |
NSLog(@"Hiba a lekérdezéskor %@, %@", error, [error userInfo]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment