0

I have one local UIDocument that contain save NSArray of recently views articles into single file.

My app is UITabbarController based, In one tab there is list of fetched articles from internet, the other one is UITableView backed with this UIDocument. Everything load just fine the problem is UIDocument didn't update once it has open. When I use Core data and NSFetchedResultController it work seamlessly with those controllerWillChangeContent. Is there any similar one for UIDocument ?

sarunw
  • 8,036
  • 11
  • 48
  • 84

1 Answers1

0

UIDocument provides updateChangeCount for this. Send this to your UIDocument (subclass) after you made a change or let the UIDocument listen to notifications sent from viewcontrollers with the selector set appropriately .

E.g. in the init of your UIDocument class:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(somethingChanged) name:@"DocumentDidChangeNotification" object:nil];

Also in UIDocument:

- (void)somethingChanged { [self updateChangeCount:UIDocumentChangeDone]; }

And in a view controller, when the content changed, do:

[[NSNotificationCenter defaultCenter] postNotificationName:@"DocumentDidChangeNotification" object:nil];

SAE
  • 1,609
  • 2
  • 18
  • 22
  • I think updateChangeCount is for auto save feature, and this doesn't answer how to sync the state between two UIDocument opened. – sarunw Jan 10 '13 at 04:17
  • Why *two* UIDocuments? Your question is about *one* UIDocument. What's wrong with auto save? That's the way to do it as also explained [here](https://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIDocument_Class/UIDocument/UIDocument.html#//apple_ref/doc/uid/TP40010879) – SAE Jan 10 '13 at 14:00
  • I mean one custom UIDocument class, but two instance open this same document (one open for save recently view article and one open for viewing them in second tab). After I add new article to the list, the UIDocument instance in second tab not update to reflect this change. – sarunw Jan 10 '13 at 14:30
  • That's not a very good idea. One document should only be managed by one instance. You shouldn't have a second instance of the document, but work with one and the same instance in both view controllers. – SAE Jan 10 '13 at 14:43
  • Any suggestion ? Because it always open at Tab 2 which is UITableView of recently view article. And when users browse articles in Tab 1 I have to update the document to add a new article into the list. – sarunw Jan 10 '13 at 17:58
  • That's o.k., but just don't have two instances of the same document. E.g. open the document in your AppDelegate (create an instance) and use this instance from your viewcontrollers. Updates then work like I stated in my answer. – SAE Jan 10 '13 at 18:05
  • There's a problem with that. If your app crashes before you close the document, you will lose everything. – Paulo Cesar May 26 '16 at 16:14