patternMinor
Simple iPhone Notes app
Viewed 0 times
notessimpleappiphone
Problem
I'm a beginner developer, I'm learning online (mostly from the Apple documentation guides), and couple a days ago I started a notes app project. It's the first app I'm building without a guide.
It's very simple so I'd love to post it here and get your feedback on how to improve my code to be more efficient.
The app have two view controllers:
-
NotesListViewController
-
CreateNotesViewController
I use Core Data so I created an entity called
So those are my class:
NotesListViewController.h:
NotesListViewController.m:
```
#import "NotesListViewController.h"
#import "Note.h"
#import "CreateNotesViewController.h"
@interface NotesListViewController ()
@property (nonatomic, strong) NSMutableArray *notes;
@property (nonatomic) NSInteger editedRow;
@end
@implementation NotesListViewController
{
NSManagedObjectContext *context = nil;
id delegate = [[UIApplication sharedApplication] delegate];
if ([delegate performSelector:@selector(managedObjectContext)]){
context = [delegate managedObjectContext];
}
return context;
}
{
if ([[segue identifier] isEqualToString:@"editSegue"]) {
CreateNotesViewController destination = (CreateNotesViewController )[segue destinationViewController];
NSInteger indx = [self.tableView indexPathForCell:sender].row;
Note *noteToPass = self.notes[indx];
destination.note = noteToPass;
self.editedRow = indx;
NSManagedObject *selectedNote = [self.notes objectAtIndex:[self.tableView indexPathForSelectedRow].row];
destination.editN
It's very simple so I'd love to post it here and get your feedback on how to improve my code to be more efficient.
The app have two view controllers:
-
NotesListViewController
-
CreateNotesViewController
I use Core Data so I created an entity called
Note that have one string attribute called content, after that I created the model class from the entity section by doing editor/create NSManagedObject subclass.So those are my class:
NotesListViewController.h:
#import
@interface NotesListViewController : UITableViewController
- (IBAction) unwindToList:(UIStoryboardSegue *)segue;
@endNotesListViewController.m:
```
#import "NotesListViewController.h"
#import "Note.h"
#import "CreateNotesViewController.h"
@interface NotesListViewController ()
@property (nonatomic, strong) NSMutableArray *notes;
@property (nonatomic) NSInteger editedRow;
@end
@implementation NotesListViewController
- (NSManagedObjectContext *) managedObjectContext
{
NSManagedObjectContext *context = nil;
id delegate = [[UIApplication sharedApplication] delegate];
if ([delegate performSelector:@selector(managedObjectContext)]){
context = [delegate managedObjectContext];
}
return context;
}
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"editSegue"]) {
CreateNotesViewController destination = (CreateNotesViewController )[segue destinationViewController];
NSInteger indx = [self.tableView indexPathForCell:sender].row;
Note *noteToPass = self.notes[indx];
destination.note = noteToPass;
self.editedRow = indx;
NSManagedObject *selectedNote = [self.notes objectAtIndex:[self.tableView indexPathForSelectedRow].row];
destination.editN
Solution
Look into the NSFetchedResultsController to power your table view. It's something specifically designed to be used with UITableViews that are powered by Core Data.
In essence, it uses a fetch request and returns how many objects are in each section, how many sections there are, what header/footer information they have etc. It has the added benefit that if the underlying managed object context changes (for example when a new object is added, updated or deleted) the fetched results controller is updated automatically. When that happens you can send your table view a reloadData message so it's updated as if by magic. An NSFetchedResultsController adds another level of complexity to Core Data, but since you're already confidently using the framework my guess is you'll like it ;-)
As a side note: look into Protocols. UIKit uses them extensively, they can be very useful and they're easy to create. With Protocols you can put one controller in charge of presenting and dismissing another view controller without having to rely on an unwind segue. The benefit is that one controller can hold on to a managed object, it gets changed in another view controller, and when that's dismissed the originating controller can use the changed object (to save or evaluate it).
I've got a full working project on GitHub which illustrates both the NSFetchedResultsController and uses a Protocol to dismiss the Detail View. It's similar to what you're building (a CRUD Project - as in Create, Read, Update, Delete):
https://github.com/versluis/Master-Detail-CRUD
Then of course there's adding a search feature to your table view... but that's for another time. When you're ready, I have a demo project for that too: https://github.com/versluis/TableSearch2014
Let me know if it helps, and have fun with Objective C!
PS: Kudos for sharing, and for not starting from a template. Well done!
In essence, it uses a fetch request and returns how many objects are in each section, how many sections there are, what header/footer information they have etc. It has the added benefit that if the underlying managed object context changes (for example when a new object is added, updated or deleted) the fetched results controller is updated automatically. When that happens you can send your table view a reloadData message so it's updated as if by magic. An NSFetchedResultsController adds another level of complexity to Core Data, but since you're already confidently using the framework my guess is you'll like it ;-)
As a side note: look into Protocols. UIKit uses them extensively, they can be very useful and they're easy to create. With Protocols you can put one controller in charge of presenting and dismissing another view controller without having to rely on an unwind segue. The benefit is that one controller can hold on to a managed object, it gets changed in another view controller, and when that's dismissed the originating controller can use the changed object (to save or evaluate it).
I've got a full working project on GitHub which illustrates both the NSFetchedResultsController and uses a Protocol to dismiss the Detail View. It's similar to what you're building (a CRUD Project - as in Create, Read, Update, Delete):
https://github.com/versluis/Master-Detail-CRUD
Then of course there's adding a search feature to your table view... but that's for another time. When you're ready, I have a demo project for that too: https://github.com/versluis/TableSearch2014
Let me know if it helps, and have fun with Objective C!
PS: Kudos for sharing, and for not starting from a template. Well done!
Context
StackExchange Code Review Q#47645, answer score: 2
Revisions (0)
No revisions yet.