Date of last entity change

Is there any solution to get the latest modification date of all Enity? Each timestamp NSManagedObjectwill not be a good solution due to the synchronization of applications with the web service (some of them will be in the database with the old date).

I need to create a timestamp for the whole entity every time the user decides to update the list so that I can present this date in SSPullToRefresh.

There is only one solution in my mind: NSUserDefaultsbut is it good to keep the latest modification date of Entity?

+4
source share
4 answers

NSUserDefaults .

​​, NSPersistentStore. , -.

NSPersistentStoreCoordinator, . , (, - ), .

+1

- . :

LastModified:
    date (NSDate*)
    key (NSString*)

key

( ).
, key objectID .

:
, , , , , ( ).

, , , , , LastModified, ( ). < /" >

:

  • , / .
  • KVO .
  • , .

:

  • CoreData , key ( , ), ( userInfo key, ).
+2

, , - .

, , , . lastModifiedDate . - , -.

, -

, , , , . , "".

+1

You can keep it simple and use NSUserDefaults. You can add an observer to the notification NSManagedObjectContextWillSave. Then create / update the object NSDateusing the NSStringname Class. I have this in mine AppDelegate:

In the app: didFinishLaunchingWithOptions:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(mocWillSave:)
                                             name:NSManagedObjectContextWillSaveNotification
                                           object:self.managedObjectContext];

Selector

- (void)mocWillSave:(NSNotification*)note {
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    // Get array of classes that have been updated.
    // This way, we don't loop through every updated Object...just the 
    // unique classes in the updatedObjects set.
    NSArray *updatedObjectClasses = [self.managedObjectContext.updatedObjects valueForKeyPath:@"class"];

    for (id obj in updatedObjectClasses) {
        [userDefaults setObject:[NSDate date] forKey:obj];
    }

    [userDefaults synchronize];
}

NOTE: This, of course, applies only to the account updatedObjects. Of course you can do it for insertedObjects.

+1
source

Source: https://habr.com/ru/post/1528828/


All Articles