1

I am making a method that will remove all of my NSManagedObjects that were not updated in the last sync.

- (void)removeStaleObjects {

        // Delete objects that have not been updated in this sync.
    NSPredicate *stalePredicate = [NSPredicate predicateWithFormat:@"updated < %@", self.syncStart];
    NSArray *staleObjects = [Node MR_findAllWithPredicate:stalePredicate];

    for (Node *n in staleObjects) {
        [[NSManagedObjectContext MR_defaultContext] deleteObject:n];
    }

}

The code keeps failing on the MR_findAll... line with

[__NSDate objCType]: unrecognized selector sent to instance

I have checked my syntax with the apple documentation and I am 99% positive that I am creating the predicate correctly, startDate is just

_startDate = [NSDate date];

that gets run prior to my sync. then after my sync I call

[self removeStaleObjects];

Does anyone know where I am messing up?

Update: I was able to get it to work by storing the update time as a double. However, I am still interested in getting it to work with NSDates so if anyone figures something out, please post it here.

Weston
  • 1,481
  • 1
  • 11
  • 31

2 Answers2

1

It also looks to my that your predicate is formatted correctly. Here are a couple things you can do:

1) When debugging this, print out that predicate. You should see something like:

updatedDate < {some integer value}

Dates are stored as integers under the covers, and a predicate converts it properly as well. If your predicate isn't printable in the debugger, you'll know right away

2) Check your updatedDate type. Make sure that's a date (I trust it's already a date, but you didn't specify in your question)

3) Make sure your Node object has the updatedDate attribute on it.

casademora
  • 67,775
  • 17
  • 69
  • 78
  • Yea, all my date objects are populating correctly, I can print them out and they are not nil. My predicate looks like this `(lldb) po stalePredicate $2 = 0x1d564f50 updated < CAST(390414548.526138, "NSDate")` – Weston May 16 '13 at 16:33
1

The problem is the name "updated" of your attribute. It conflicts with the isUpdated method of NSManagedObject. (See Core Data NSPredicate "deleted == NO" does not work as expected for a similar issue with a "deleted" attribute.)

If you rename your attribute, everything works as expected.

Community
  • 1
  • 1
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • then why does it work if I change updated from Date to Double? – Weston May 16 '13 at 18:37
  • That might be pure chance. It could even be that the predicate works but returns wrong results. See my answer to above linked question where I showed that "strange things happen" if you call an attribute "deleted". I suppose the same applies here. – Martin R May 16 '13 at 18:39
  • Have a look at the docs, http://developer.apple.com/library/ios/Documentation/Cocoa/Conceptual/CoreData/Articles/cdManagedObjects.html#//apple_ref/doc/uid/TP40003397-SW7 If changing your attribute to updatedDate doesn't work, well, something is really hosed. – casademora May 17 '13 at 00:12
  • @casademora: It is documented that you should not override methods such as `isUpdated` (and Xcode actually warns you if you call an attribute "isUpdated"). But there is no method `updated`, so it is not obvious that an attribute "updated" could cause problems. I assume this is caused by Key-Value accessor patterns (`isFoo` can be the getter method for a Boolean property `foo`). Unfortunately, this is not well documented and Xcode does not emit a warning. – Martin R May 17 '13 at 05:06
  • @Kronusdark: Does it work for you if you rename the attribute? – Martin R May 20 '13 at 20:12
  • Just got around to revisiting that code, and yes, that was the issue. works fine with NSDate now. – Weston May 22 '13 at 17:05