Well, I have an IBAction that syncs with iCal and is also triggered by the KVO of the name property in my CD model, so when the property changes the action, the action starts. It happens that as soon as the IBAction reaches the end, it goes to the KVO declaration, which then starts the action again and again and again, a cycle occurs here.
Here is the code. IBAction ...
- (IBAction)sync:(id)sender {
[syncButton setTitle:@"Syncing..."];
NSString *dateText = (@"Last Sync : %d", [NSDate date]);
[syncDate setStringValue:dateText];
NSManagedObjectContext *moc = [self managedObjectContext];
NSEntityDescription *entityDescription = [NSEntityDescription
entityForName:@"projects" inManagedObjectContext:moc];
NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
[request setEntity:entityDescription];
NSError *error = nil;
NSArray *array = [moc executeFetchRequest:request error:&error];
if (array == nil)
{
NSAlert *anAlert = [NSAlert alertWithError:error];
[anAlert runModal];
}
NSArray *namesArray = [array valueForKey:@"name"];
NSPredicate *predicate = [CalCalendarStore taskPredicateWithCalendars:[[CalCalendarStore defaultCalendarStore] calendars]];
NSArray *tasksNo = [[CalCalendarStore defaultCalendarStore] tasksWithPredicate:predicate];
NSArray *tasks = [tasksNo valueForKey:@"title"];
NSMutableArray *namesNewArray = [NSMutableArray arrayWithArray:namesArray];
[namesNewArray removeObjectsInArray:tasks];
NSLog(@"%d", [namesNewArray count]);
NSInteger *popIndex = [calenderPopup indexOfSelectedItem];
CalCalendarStore *store = [CalCalendarStore defaultCalendarStore];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
NSString *supportDirectory = [paths objectAtIndex:0];
NSString *fileName = [supportDirectory stringByAppendingPathComponent:@"oldtasks.plist"];
NSMutableArray *oldTasks = [[NSMutableArray alloc] initWithContentsOfFile:fileName];
[oldTasks removeObjectsInArray:namesArray];
NSLog(@"%d",[oldTasks count]);
NSPredicate* taskPredicate = [CalCalendarStore taskPredicateWithCalendars:[[CalCalendarStore defaultCalendarStore] calendars]];
NSArray* allTasks = [[CalCalendarStore defaultCalendarStore] tasksWithPredicate:taskPredicate];
CalCalendar *calendar = [[store calendars] objectAtIndex:popIndex];
for(NSString *title in namesNewArray) {
CalTask *task = [CalTask task];
task.title = title;
task.calendar = calendar;
if(![[CalCalendarStore defaultCalendarStore] saveTask:task error:&error]) {
NSLog(@"Error");
NSAlert *anAlert = [NSAlert alertWithError:error];
[anAlert runModal];
}
}
NSMutableArray *tasksNewArray = [NSMutableArray arrayWithArray:tasks];
[tasksNewArray removeObjectsInArray:namesArray];
NSLog(@"%d", [tasksNewArray count]);
for(NSString *title in tasksNewArray) {
NSManagedObjectContext *moc = [self managedObjectContext];
JGManagedObject *theParent =
[NSEntityDescription insertNewObjectForEntityForName:@"projects"
inManagedObjectContext:moc];
[theParent setValue:nil forKey:@"parent"];
[theParent setValue:title forKey:@"name"];
[theParent setValue:[NSNumber numberWithInt:0] forKey:@"position"];
}
for(CalTask* task in allTasks)
if([oldTasks containsObject:task.title]) {
[store removeTask:task error:nil];
}
NSPredicate *mocPredicate = [NSPredicate predicateWithFormat:@"name IN %@", oldTasks];
[request setPredicate:mocPredicate];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
[request setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
NSArray *resultArray = [moc executeFetchRequest:request error:&error];
if (resultArray == nil)
{
NSAlert *anAlert = [NSAlert alertWithError:error];
[anAlert runModal];
}
for (JGManagedObject *objectToDelete in resultArray ) {
[moc deleteObject:objectToDelete];
}
[namesArray writeToFile:fileName atomically:YES];
[syncButton setTitle:@"Sync Now"];
NSLog(@"Sync Completed");
}
That when it reaches the end (somehow), it starts the KVO declaration ...
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
[self performSelector:@selector(sync:)];
}
Which then starts IBAction again. A cycle takes place here, because it gets stuck here and constantly causes an action.
Can anyone understand what is happening on Earth ???
source
share