-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathListViewController.m
More file actions
173 lines (143 loc) · 6.46 KB
/
ListViewController.m
File metadata and controls
173 lines (143 loc) · 6.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
//
// ListViewController.m
// Template 1
//
// Created by Rafael on 05/12/13.
// Copyright (c) 2013 Rafael Colatusso. All rights reserved.
//
#import "ListViewController.h"
#import "MenuTableViewController.h"
#import "DataStore.h"
#import "ToDoList.h"
@interface ListViewController ()
@end
@implementation ListViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// listen for new rows added via AddToDoViewController
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleUpdatedData:)
name:@"ToDoDataUpdated"
object:nil];
[self reloadDataFromParse];
}
- (void) viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self reloadDataFromParse];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [self.toDoArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
ToDoList *object = [self.toDoArray objectAtIndex:indexPath.row];
cell.textLabel.text = object.text;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// delete the row from parse and fade out the cell
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
PFObject *object = [PFObject objectWithoutDataWithClassName:@"ToDoList" objectId:[[self.toDoArray objectAtIndex:indexPath.row] objectId]];
// we need to save the objectId before deleting it from parse
__block NSString *toDeleteId = [object objectId];
[object deleteInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (!error) {
[UIView animateWithDuration:1.0f animations:^{
cell.alpha = 0;
} completion:^(BOOL finished) {
DataStore *dataStore = [DataStore sharedInstance];
NSManagedObjectContext *context = [dataStore storeContext];
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"ToDoList"];
request.predicate = [NSPredicate predicateWithFormat:@"objectId == %@", toDeleteId];
NSArray *toDelete = [context executeFetchRequest:request error:nil];
[context deleteObject:toDelete[0]];
[context save:nil];
[self reloadDataFromParse];
}];
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Unable to connect to parse" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
}];
cell.selected = NO;
}
#pragma mark -
#pragma mark Helper methods
- (void)reloadDataFromParse {
// The sync work in both ways, add or remove an object at parse's backend and
// the change will reflect locally, if you add or remove in the app
// the change will reflect at parse's backend.
// It works with insert and delete, I'm already working on the "update".
// let´s start by syncing the local data with parse
self.toDoArray = [NSArray array];
DataStore *dataStore = [DataStore sharedInstance];
NSManagedObjectContext *context = [dataStore storeContext];
// load the locally data first and displaying it
NSFetchRequest *newRequest = [NSFetchRequest fetchRequestWithEntityName:@"ToDoList"];
self.toDoArray = [context executeFetchRequest:newRequest error:nil];
[self.tableView reloadData];
// then sync with parse and reload the tableview
NSFetchRequest *idRequest = [NSFetchRequest fetchRequestWithEntityName:@"ToDoList"];
[idRequest setPropertiesToFetch:[NSArray arrayWithObjects:@"objectId", nil]];
NSMutableArray *idArray = [[context executeFetchRequest:idRequest error:nil] valueForKey:@"objectId"];
NSMutableArray *toCompareArray = [NSMutableArray array];
self.query = [PFQuery queryWithClassName:@"ToDoList"];
[self.query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if(!error) {
for (PFObject *tmp in objects) {
[toCompareArray addObject:[tmp objectId]];
if (![idArray containsObject:[tmp objectId]]) {
ToDoList *newToDo = [NSEntityDescription insertNewObjectForEntityForName:@"ToDoList"
inManagedObjectContext:context];
newToDo.objectId = [tmp objectId];
newToDo.text = [tmp objectForKey:@"text"];
NSError *storeError;
[context save:&storeError];
}
}
// if you have removed an object directly on parse, we have to remove it locally
for (NSString *toDeleteId in idArray) {
if (![toCompareArray containsObject:toDeleteId]) {
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"ToDoList"];
request.predicate = [NSPredicate predicateWithFormat:@"objectId == %@", toDeleteId];
NSArray *toDelete = [context executeFetchRequest:request error:nil];
[context deleteObject:toDelete[0]];
[context save:nil];
}
}
// reload the tableview with the new data
NSFetchRequest *newRequest = [NSFetchRequest fetchRequestWithEntityName:@"ToDoList"];
self.toDoArray = [context executeFetchRequest:newRequest error:nil];
[self.tableView reloadData];
}
}];
}
-(void)handleUpdatedData:(NSNotification *)notification {
// when notificated reload the data
[self reloadDataFromParse];
}
@end