code_junkie

Dealing with variable assignments and async requests

Nov 13th, 2011
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. users = [MySingleton sharedInstance].users;
  2.  
  3. - (id)getUsersAndNotifyObject:(id)object selector:(SEL)selector
  4.  
  5. // register observer first so you don't miss an update
  6. [[MySingleton sharedInstance] addObserver:self
  7. forKeyPath:@"users"
  8. options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld)
  9. context:&kvo_users_context];
  10. users = [MySingleton sharedInstance].users;
  11.  
  12. // implement appropriate observeValueForKeyPath:ofObject:change:context: method
  13.  
  14. [[NSNotificationCenter defaultCenter] addObserver:self
  15. selector:@selector(sharedDataChanged:)
  16. name:MySingletonDataUpdatedNotification
  17. object:[MySingletonDataUpdatedNotification sharedInstance]];
  18. users = [MySingleton sharedInstance].users;
  19.  
  20. // implement appropriate sharedDataChanged: method
  21.  
  22. users = [MyDataFactory getUsers];
  23.  
  24. @interface RecipeRepository {}
  25. - (void)initWithDelegate:(id)aDelegate;
  26. - (void)findAllRecipes;
  27. - (void)findRecipeById:(NSUInteger)anId;
  28. @end
  29.  
  30. @protocol RepositoryDelegateProtocol
  31. - (void)repository:(id)repository didRetrieveEntityCollection:(NSArray *)collection;
  32. - (void)repository:(id)repository didRetrieveEntity:(id)entity;
  33. @end
  34.  
  35. - (void)viewDidLoad
  36. {
  37. self.users = [MySingleton getUsers];
  38. [self.view setNeedsDisplay];
  39. }
  40.  
  41. - (void)viewDidLoad
  42. {
  43. if(self.repository == nil) { // just some simple lazy loading, we only need one repository instance
  44. self.repository = [[[RecipeRepository alloc] initWithDelegate:self] autorelease];
  45. }
  46. [self.repository findAllRecipes];
  47. }
  48.  
  49. - (void)repository:(id)repository didRetrieveEntityCollection:(NSArray *)collection;
  50. {
  51. self.users = collection;
  52. [self.view setNeedsDisplay];
  53. }
  54.  
  55. @protocol RepositoryDelegateProtocol
  56. - (void)repositoryWillLoadEntities:(id)repository;
  57. @end
  58.  
  59. // in your controller
  60.  
  61. - (void)repositoryWillLoadEntities:(id)repository;
  62. {
  63. [self showLoadingView]; // etc.
  64. }
Add Comment
Please, Sign In to add comment