I have a UICollectionView that needs to be updated when the model has changed.
It is subscribed to the notification center in order to do so:
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(modeloActualizado:) name:NSManagedObjectContextObjectsDidChangeNotification object:self.contexto];
}
The UICollectionViewController connects to the model through a NSFetchedResultsController that is meant to return a list of the entities "Plaza" whose "ocupada" value is 1 (it is a boolean).
-(NSFetchedResultsController*) frController{
if(_frController == nil){
NSFetchRequest *request = [[NSFetchRequest alloc]init];
NSEntityDescription *entidad = [NSEntityDescription entityForName:#"Plaza" inManagedObjectContext:self.contexto];
request.entity = entidad;
request.fetchBatchSize = 10;
// Añadir un predicado para filtrar por plazas ocupadas
request.predicate = [NSPredicate predicateWithFormat:#"ocupada == 1"];//%#",[NSNumber numberWithBool:YES]];
// Ordenar por numero de plaza
NSSortDescriptor *ordenPorNumero = [[NSSortDescriptor alloc] initWithKey:#"numero" ascending:YES];
NSArray *descriptores = [[NSArray alloc] initWithObjects:ordenPorNumero, nil];
[request setSortDescriptors:descriptores];
// Crear el FetchedResultsController
//[NSFetchedResultsController deleteCacheWithName:#"Coleccion"];
_frController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:self.contexto sectionNameKeyPath:nil cacheName:#"Coleccion"];
_frController.delegate = self;
NSError *error = nil;
if(![self.frController performFetch:&error]){
NSLog(#"Ha ocurrido un error: %# %#",error,[error userInfo]);
abort();
}
}
return _frController;
}
The method called when the model changed does reload the CollectionView data:
-(void)modeloActualizado:(NSNotification *) notificacion{
[self.collectionView reloadData];
}
The problem is, if I change any of the values of an entity, those changes are shown in the CollectionView, but if I change the entity boolean value "ocupada" to NO, it will still appear into the CollectionView until the App is closed and opened again.
Am I doing something wrong? I don't know why the NSFetchedResultsController is returning the same amount of objects even though one of them no longer matches the predicate condition.
Any ideas?
Thanks in advance.
Related
I'm working with Core Data and web service, I want to add my data to my table,
but I don't know how should I call them, would you please help me, since when I used this way it's not working.
Here is my method for update database in my HTTP class
- (void)updateLocalCardsDataBase:(NSArray*) cardsArray
{
//check if current user has cards in local database
NSManagedObjectContext* managedObjectContext = [(AppDelegate*) [[UIApplication sharedApplication] delegate] managedObjectContext];
for(NSDictionary *cardDic in cardsArray)
{
Card *card = [NSEntityDescription insertNewObjectForEntityForName:#"Card" inManagedObjectContext:managedObjectContext];
card.remote_id = [NSNumber numberWithInt:[[cardDic objectForKey:#"id"] intValue]];
card.stampNumber = [NSNumber numberWithInt:[[cardDic objectForKey:#"stampNumber"] intValue]];
card.createdAt = [NSDate dateWithTimeIntervalSince1970:[[cardDic objectForKey:#"createdAt"] intValue]];
[managedObjectContext lock];
NSError *error;
if (![managedObjectContext save:&error])
{
NSLog(#"Whoops, couldn't save: %#", [error localizedDescription]);
NSLog(#"Failed to save to data store: %#", [error localizedDescription]);
NSArray* detailedErrors = [[error userInfo] objectForKey:NSDetailedErrorsKey];
if(detailedErrors != nil && [detailedErrors count] > 0) {
for(NSError* detailedError in detailedErrors) {
NSLog(#" DetailedError: %#", [detailedError userInfo]);
}
}
else {
NSLog(#" %#", [error userInfo]);
}
}
[managedObjectContext unlock];
}
Here is my table:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
// NSManagedObjectContext* managedObjectContext = [(AppDelegate*) [[UIApplication sharedApplication] delegate] managedObjectContext];
static NSString *CellIdentifier = #"CardsCell";
CardCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil){
NSArray *objects = [[NSBundle mainBundle] loadNibNamed:#"CardCell" owner:nil options:nil];
for (id currentObject in objects)
{
if([currentObject isKindOfClass:[UITableViewCell class]])
{
cell = (CardCell *) currentObject;
break;
}
}
NSDictionary *f = [_cards objectAtIndex:indexPath.row];
cell.stampId.text = [f objectForKey:#"stampNumber"];
NSLog(#"%#fdssfdfddavds",[f objectForKey:#"stampNumber"]);
cell.createdAt.text = [f objectForKey:#"createdAt"];
cell.CardId.text = [f objectForKey:#"id"];
return cell;
}
Edit:
My problem is how I can show data in a UITableView
Before call [tableView reloadData], you need to get a data source first. You will get back an array of your data models, not an NSDictionary. You can place the my example method (or a variation that suits you best) where ever best suits your needs, but this one will not filter or sort the models, it will only get all of them. Also, I will place the method in your view controller that stores the table view:
-(NSArray*)getMycards {
NSManagedObjectContext *context = [(AppDelegate*) [[UIApplication sharedApplication] delegate] managedObjectContext];
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:#"Card" inManagedObjectContext:context];
NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
NSError *error;
[request setEntity:entityDescription];
NSArray *cards = [context executeFetchRequest:request error:&error];
// now check if there is an error and handle it appropriatelty
// I usually return 'nil' but you don't have if you don't want
if ( error != nil ) {
// investigate error
}
return cards;
}
I recommend creating a property #property NSArray *cards in the view controller where you place your table, it will be easier to manage. One assumption I have made (since I have no other information about your view controller, a property named 'tableView' is declared in your view controller's header file (#property UITableView *tableView;), adjust the naming as needed.
With the above method, when you want to populate your array before loading the table's data:
// you put this block of code anywhere in the view controller that also has your table view
// likely in 'viewDidLoad' or 'viewDidAppear'
// and/or anywhere else where it makes sense to reload the table
self.cards = [self getMyCards];
if ( self.cards.count > 0 )
[self.tableview reloadData];
else {
// maybe display an error
}
Now, your cellForRowAtIndexPath should look like
-(UITableViewCell*tableView:tableView cellForRowAtIndexPath {
UITbaleViewCell *cell = ...;
// creating the type of cell seems fine to me
.
.
.
// keep in mind I don't know the exact make up of your card model
// I don't know what the data types are, so you will have to adjust as necessary
Card *card = self.cards[indexPath.row];
cell.stampId.text = [[NSString alloc] initWithFormat:#"%#",card.stamp];
cell.createdAt.text = [[NSString alloc] initWithFormat:#"%#",card.createdAt];
// you might want format the date property better, this might end being a lot more than what you want
cell.CardId.text = [[NSString alloc] initWithFormat:#"%#",card.id];
return cell;
}
Core Data is extremely powerful, I highly recommend the Core Data overview, followed by the Core Data Programming Guide.
I am using a WCF service in my app.When the app is run for the first time on the iPad,I want it to call a WCF service and display the result in a UITableView.Alongwith displaying the data in UITableView,i want to store the data in Core Data so when the user is "offline"(not connected to wifi)the data will be displayed from the Core Data.The AppDelegate.m looks like this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (![defaults objectForKey:#"firstRun"])
{
self.firstRun = TRUE;
[defaults setObject:[NSDate date] forKey:#"firstRun"];
}
else
{
self.firstRun = FALSE;//flag does exist so this ISNT the first run
}
[[NSUserDefaults standardUserDefaults] synchronize];
}
The code in UITableView looks like this:
- (void)viewDidLoad
{
[super viewDidLoad];
[my_table setDataSource:self];
[my_table setDelegate:self];
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
if (appDelegate.firstRun){
NSLog(#"IS FIRST RUN");
EDViPadDocSyncService *service = [[EDViPadDocSyncService alloc]init];
[service getAllCategories:self action:#selector(handleGetAllCategories:)];
}
else
{
NSLog(#"NOT FIRST RUN");
NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:#"Categories" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
NSError *errormsg;
self.allCats = [managedObjectContext executeFetchRequest:fetchRequest error:&errormsg];
NSLog(#"allCATS=%#",self.allCats);
self.title = #"Categories";
}
}
-(void)handleGetAllCategories:(id)value
{
if([value isKindOfClass:[NSError class]])
{
NSLog(#"This is an error %#",value);
return;
}
if([value isKindOfClass:[SoapFault class]])
{
NSLog(#"this is a soap fault %#",value);
return;
}
NSMutableArray *result = (NSMutableArray*)value;
NSMutableArray *categoryList = [[NSMutableArray alloc] init];
NSMutableArray *docCount = [[NSMutableArray alloc]init];
NSMutableArray *catIdList = [[NSMutableArray alloc]init];
self.myData = [[NSMutableArray array] init];
self.myDocCount = [[NSMutableArray array]init];
self.catId = [[NSMutableArray array]init];
for (int i = 0; i < [result count]; i++)
{
EDVCategory *catObj = [[EDVCategory alloc]init];
catObj = [result objectAtIndex:i];
[categoryList addObject:[catObj categoryName]];
[docCount addObject:[NSNumber numberWithInt:[catObj docCount]]];
[catIdList addObject:[NSNumber numberWithInt:[catObj categoryId]]];
}
self.myData = categoryList;
self.myDocCount = docCount;
self.catId = catIdList;
[my_table reloadData];
/*store data in Core Data - START*/
NSManagedObjectContext *context = [self managedObjectContext];
NSManagedObject *newCategory;
for(int j=0;j<[result count];j++)
{
newCategory = [NSEntityDescription insertNewObjectForEntityForName:#"Categories" inManagedObjectContext:context];
/*HOW TO STORE DATA FOR THE "CATEGORIES" OBJECT IN CORE DATA*/
}
/*store data in Core Data - END*/
}
I am not able to figure out how to store the data received from the wcf service to the core data object directly.I know how to store it from a text box on the screen to a core data object.eg.:-
coreDataAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSManagedObject *newContact;
newCat = [NSEntityDescription insertNewObjectForEntityForName:#"Categories" inManagedObjectContext:context];
[newCat setValue:name.text forKey:#"name"];
name.text = #"";
[context save:&error];
But this doesn't help in my case.Any help is appreciated.
You are mixing networking and UI code. It is a recipe for unmaintainable code.
Your UI should be looking at Core Data and only Core Data to display its data.
Separately, and asynchronously you should be requesting data from WCF and pushing it into Core Data.
Your UI does not need to care about first run vs. subsequent run. It just looks at Core Data via a NSFetchedResultsController.
Your network code is the only part that cares about new vs. update.
Update 1
how can I achieve this? When the app is running and connected to WiFi,it has to get the latest data from the WCF service.
NSURLConnection can do async requests built-in. I generally recommend writing your networking code as NSOperation subclasses and then put them into a queue.
It appears that WCF can return XML and takes standard HTTP requests. Therefore you can write NSOperation subclasses that build your request, send it to the server and wait for a reply. When the reply comes you parse the XML and insert it into Core Data. When you save the Core Data NSManagedObjectContext your NSFetchedResultsController instances will automatically fire and allow you to update your UI.
I have several code samples that perform these feats although they are written for JSON responses as opposed to XML responses. It would not be difficult to take those examples and alter them to your needs.
You can start with this stackoverflow question and its response.
To store the data into the attributes of your NSManagedObject, simply set the values using KVC:
EDVCategory *catObject = [result objectAtIndex:j];
[newCategory setValue:[catObject categoryName] forKey#"categoryName"];
[newCategory setValue:[catObject docCount] forKey#"docCount"];
[newCategory setValue:[catObject categoryID] forKey#"categoryID"];
// after the loop
[context save:&nil];
My app use CoreData. It has a UISegmentedControl and a tableview. The UISegmentedControl is used to update tableview based on different criteria.
The 1st option of UISegmentedControl will show all data in a single tableview section. So I use the following code in - (NSFetchedResultsController*)fetchedResultsController
frc = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:[self managedObjectContext]
sectionNameKeyPath:nil
cacheName:#"Root"];
The 2nd option will show data in multiple tableview section. So I need NSSortDescriptor and set sectionNameKeyPath.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"city"
ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
NSFetchedResultsController *frc = nil;
frc = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:[self managedObjectContext]
sectionNameKeyPath:#"city"
cacheName:#"Root"];
Because sectionNameKeyPath is different which each of them need different NSSortDescriptor, I assume I need two different NSFetchedResultsController.
The code I use to reload tableview:
- (void) touchDownAtSegmentIndex:(NSUInteger)segmentIndex{
NSPredicate *predicate;
NSError *error;
[NSFetchedResultsController deleteCacheWithName:#"Root"];
if(segmentIndex == 0)
{
}
else if(segmentIndex == 1)
{
}
else
{
return;
}
[[[self fetchedResultsController] fetchRequest] setPredicate:predicate];
[[self fetchedResultsController] performFetch:&error];
[[self tableView] reloadData];
}
Anyone can provide me a sample of code that use two NSFetchedResultsController.
Thank you.
SOLUTION:
In addition from #iTukker idea, I added more checking before returning fetchedResultsController. Without this checking, this method will return fetchedResultsController instead of executing switch .. case .. statement.
- (NSFetchedResultsController*)fetchedResultsController {
.....
if ((fetchedResultsController!= nil) && [self.segmentChanged isEqualToString:#"false"])
return fetchedResultsController;
.....
if (frc == nil) {
switch (segmentendControl.selectedSegmentIndex) {
case 0: {
frc = ... //first frc
[frc retain];
break;
}
case 1: {
frc = ... //second frc
[frc retain];
break;
}
}
}
....
}
What you would do is something like this, assuming that frc is a instance variable of your controller
- (NSFetchedResultsController*)fetchedResultsController {
if (frc == nil) {
switch (segmentendControl.selectedSegmentIndex) {
case 0: {
frc = ... //first frc
[frc retain];
break;
}
case 1: {
frc = ... //second frc
[frc retain];
break;
}
}
}
return frc;
}
And when the value of your segmentedController changes
[frc release];
frc = nil;
[tableView reloadData];
Hope this makes sence
WHAT I HAVE SO FAR:
In one splitview, I have a a tableview as its master, and a UIView as the detail. The tableview has 2 columns: "Days" and then "Sessions". I get the data from the Core Data, the entities called "Sessions". When I click on a "Session" tableviewcell, the detailview gets updated.
In the detailview, I added an "Add" button in the navigation bar. When you click on this, I add a new entity called "NewSession" to the core data.
if ([_sessionData.added isEqualToNumber:[NSNumber numberWithBool:NO]]) {
[_sessionData setValue:[NSNumber numberWithBool:YES] forKey:#"added"];
SessionData *session = (SessionData*) [NSEntityDescription insertNewObjectForEntityForName:#"NewSessions" inManagedObjectContext:[DataSingleton sharedMySingleton].managedObjectContext];
session.startDate = _sessionData.startDate;
session.endDate = _sessionData.endDate;
session.sessionLocation = nil;
session.sessionTitle = _sessionData.sessionTitle;
session.sessionDescription = _sessionData.sessionDescription;
[session setValue: [NSNumber numberWithBool:YES] forKey:#"added"];
_addButton = [[[UIBarButtonItem alloc] initWithTitle:#"Remove" style:UIBarButtonSystemItemAdd target:self action:nil] autorelease];
NSError *error = nil;
if (![[DataSingleton sharedMySingleton].managedObjectContext save:&error]) {
DebugLog(#"Whoops, couldn't save:%#", [error localizedDescription]);
}
}
else {
NSLog(#"SESSION ALREADY ADDED");
}
ANOTHER splitview's tableview fetches the "NewSession" entity, and gets all the data and displays it.
THE PROBLEM:
Whenever I exit the application and relaunch it, the sessions in the other splitview are still there, BUT I can add the SAME session again.
In the "add" code, I have the following:
[_sessionData setValue:[NSNumber numberWithBool:YES] forKey:#"added"];
Now, my sessionData is an NSManagedObject; and I thought that just setting the values will update them in the core data.
Can anyone help?
I had similar problem, but similar doesn't mean the same. I don't know if it works for you but you can try it. This method was described to me by #macbirdie and it works for me.
First of all, import your AppDelegate header file:
#import "YourAppDelegate.h"
Then, update your code:
if ([_sessionData.added isEqualToNumber:[NSNumber numberWithBool:NO]]) {
[_sessionData setValue:[NSNumber numberWithBool:YES] forKey:#"added"];
NSManagedObjectContext *moc = [[DataSingleton sharedMySingleton] managedObjectContext];
SessionData *session = (SessionData*) [NSEntityDescription insertNewObjectForEntityForName:#"NewSessions" inManagedObjectContext:moc];
session.startDate = _sessionData.startDate;
session.endDate = _sessionData.endDate;
session.sessionLocation = nil;
session.sessionTitle = _sessionData.sessionTitle;
session.sessionDescription = _sessionData.sessionDescription;
session.added = [NSNumber numberWithBool:YES];
_addButton = [[[UIBarButtonItem alloc] initWithTitle:#"Remove" style:UIBarButtonSystemItemAdd target:self action:nil] autorelease];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(saveMoc:) name:NSManagedObjectContextDidSaveNotification object:moc];
NSError *error = nil;
if (![moc save:&error]) {
DebugLog(#"Whoops, couldn't save:%#", [error localizedDescription]);
}
} else {
NSLog(#"SESSION ALREADY ADDED");
}
And add this methode somewhere in your file:
- (void)saveMoc:(NSNotification *)notification {
YourAppDelegate *appDel = (YourAppDelegate *)[[UIApplication sharedApplication] delegate];
[appDel.managedObjectContext mergeChangesFromContextDidSaveNotification:notification];
}
Now this may sound like my earlier problem/question but I've changed and tried a few things that were answered in my other questions to try to make it work, but I've still got the same problem.
I am observing a core data property from within a NSManagedObject sub-class and the method that gets called when the property changes calls another method but in this method it adds Core Data objects which triggers the KVO method which triggers the method again and so forth. Or so it seems, I'm not too sure about that because something different seems to happen, here is the series of events …
I click a button syncing with iCal (this in an IBAction with the exact same code thats in the method syncKVO). This sync works fine.
I add an object to my outline view. All is well.
I change its name which triggers the KVO Declaration (because I changed the 'name' property) which syncs with iCal. Works fine.
I delete the object I just added and somehow it triggers the KVO declaration (thus triggering the method) and puts me into an infinite loop.
Now for some code.
Code inside the NSManagedObject Subclass (called JGManagedObject) …
- (void) awakeFromFetch {
[self addObserver:[NSApp delegate] forKeyPath:#"name" options:0 context:nil];
}
- (void) awakeFromInsert {
[self addObserver:[NSApp delegate] forKeyPath:#"name" options:0 context:nil];
}
+ (void) addObserver{
[self addObserver:[NSApp delegate] forKeyPath:#"name" options:0 context:nil];
}
+ (void) removeObserver{
[self removeObserver:[NSApp delegate] forKeyPath:#"name"];
}
The KVO Declaration (inside the App Delegate) …
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([keyPath isEqualToString:#"name"]) {
[self performSelector:#selector(syncKVO:)];
}
}
The Method (also inside the App Delegate)…
- (void)syncKVO:(id)sender {
NSManagedObjectContext *moc = [self managedObjectContext];
[syncButton setTitle:#"Syncing..."];
NSString *dateText = (#"Last Sync : %d", [NSDate date]);
[syncDate setStringValue:dateText];
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];
//Load the array
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]);
//Use the content
NSPredicate* taskPredicate = [CalCalendarStore taskPredicateWithCalendars:[[CalCalendarStore defaultCalendarStore] calendars]];
NSArray* allTasks = [[CalCalendarStore defaultCalendarStore] tasksWithPredicate:taskPredicate];
// Get the calendar
CalCalendar *calendar = [[store calendars] objectAtIndex:popIndex];
// Note: you can change which calendar you're adding to by changing the index or by
// using CalCalendarStore's -calendarWithUID: method
// Loop, adding tasks
for(NSString *title in namesNewArray) {
// Create task
CalTask *task = [CalTask task];
task.title = title;
task.calendar = calendar;
// Save task
if(![[CalCalendarStore defaultCalendarStore] saveTask:task error:&error]) {
NSLog(#"Error");
// Diagnostic error handling
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"];
// This is where you add the title from the string array
[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];
}
// Create a predicate for an array of names.
NSPredicate *mocPredicate = [NSPredicate predicateWithFormat:#"name IN %#", oldTasks];
[request setPredicate:mocPredicate];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"name" ascending:YES];
[request setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
// Execute the fetch request put the results into array
NSArray *resultArray = [moc executeFetchRequest:request error:&error];
if (resultArray == nil)
{
// Diagnostic error handling
NSAlert *anAlert = [NSAlert alertWithError:error];
[anAlert runModal];
}
// Enumerate through the array deleting each object.
// WARNING, this will delete everything in the array, so you may want to put more checks in before doing this.
for (JGManagedObject *objectToDelete in resultArray ) {
// Delete the object.
[moc deleteObject:objectToDelete];
}
//Save the array
[namesArray writeToFile:fileName atomically:YES];
[syncButton setTitle:#"Sync Now"];
NSLog(#"Sync Completed");
}
What I've tried …
Filtering the Keypaths that call the KVO Declaration with
if ([keyPath isEqualToString:#"name"]) {
…
}
Detaching and reattaching observers with
[JGManagedObject removeObserver];
//and
[JGManagedObject addObserver];
but with that it works the first time but stops the method the second time saying that it cannot remove the observer because it is not observing, which doesn't make sense because I added the observer again the first time. That is why I left this code out of the actual method else it would stop on the second sync.
I'm not sure whats going on with this, I think I've tried everything. Whats gone wrong?
Any help would be greatly appreciated.
The problem might be here:
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([keyPath isEqualToString:#"name"]) {
[self performSelector:#selector(syncKVO:)];
}
}
You call syncKVO: everytime something happens to 'name' regardless of what it is that has actually happened to 'name'. I suggest you start using the object, change and context parameters to determine what has just happened and what action, if any, should be undertaken.
BTW, it's not considered good practice to add a lot of stuff to the app delegate. You might want to put all this syncing stuff into a proper controller class and call [NSApp delegate] when you need it.