Advertisement
Guest User

Untitled

a guest
Oct 24th, 2014
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. # PersistenceService.h
  2. #import <Foundation/Foundation.h>
  3.  
  4. @interface PersistenceService : NSObject
  5.  
  6. @property (nonatomic, retain) NSManagedObjectContext *context;
  7.  
  8. -(instancetype) init;
  9. -(void) saveContext;
  10. @end
  11.  
  12. # PersistenceService.m
  13. @implementation PersistenceService
  14.  
  15. -(instancetype) init {
  16. self = [super init];
  17.  
  18. if (self) {
  19. self.context = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
  20. self.context.parentContext = [DataManager sharedInstance].managedObjectContext;
  21. }
  22.  
  23. return self;
  24. }
  25.  
  26. -(void) saveContext {
  27. NSManagedObjectContext *context = self.context.parentContext;
  28.  
  29. [self.context performBlock:^{
  30.  
  31. NSError *error;
  32. [self.context save:&error];
  33.  
  34. [context performBlock:^{
  35. NSError *error;
  36. [context save:&error];
  37.  
  38. [context.parentContext performBlock:^{
  39. NSError *error;
  40. [context.parentContext save:&error];
  41.  
  42. }];
  43. }];
  44. }];
  45.  
  46. }
  47.  
  48. @end
  49.  
  50. # PseudoLeadPersistenceService.m
  51. -(void) saveOrUpdatePseudoLead:(Lead *)lead ofType:(PseudoLeadType)type {
  52. PseudoLeadDAO *pseudoLeadDAO = [PseudoLeadDAO findPseudoLeadByCode:lead.qrcode.code andEventId:lead.event.idEvent inContext:self.context];
  53. if (pseudoLeadDAO == nil) {
  54. pseudoLeadDAO = [[PseudoLeadDAO alloc] initWithLead:lead andType:type andContext:self.context];
  55. } else {
  56. [pseudoLeadDAO setAttributesWithLead:lead];
  57. }
  58. [self saveContext];
  59. }
  60.  
  61. # PseudoLeadDAO.m
  62. -(void) setAttributesWithLead:(Lead *)lead {
  63. [self setPseudoLeadID:lead.id];
  64. [self setHotlead:lead.hotlead];
  65. [self setPseudoLeadDescription:lead.description];
  66. [self setEvent:[EventDAO findEventById:lead.event.idEvent inContext:self.managedObjectContext]];
  67. if (lead.qrcode.code != nil) {
  68. [self setQrCode:lead.qrcode.code];
  69. }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement