Commits

José Manuel Sánchez Peñarroja committed b38437a

Added parserStrategy and compare strategy

Comments (0)

Files changed (9)

Example/Example.xcworkspace/xcuserdata/josanchez.xcuserdatad/UserInterfaceState.xcuserstate

Binary file modified.

bmf/shared/base/BMFTypes.h

 if(!(condition)) { DDLogError(@"Invalid condition not satisfying: %s", #condition); return nil;} } while(0)
 
 #define BMFInvalidInit(correctInitName) do { [NSException raise:@"Invalid constructor; some parameters are needed. Use " #correctInitName " instead" format:nil]; return nil; } while (0)
-
+#define BMFAbstractMethod() do { [NSException raise:@"Error: subclass should implement this method. Are you using the abstract class?" format:nil]; } while (0)
 
 #pragma mark Constants
 

bmf/shared/data/parsers/BMFObjectParserProtocol.h

 
 #import <Foundation/Foundation.h>
 
+@protocol BMFObjectParserDelegateProtocol <NSObject>
+
+@optional
+
+- (void) didCreateObject:(id) object;
+- (void) didParseObject:(id)object withDictionary:(NSDictionary *) dictionary;
+- (void) willDeleteObject:(id) object;
+
+@end
+
 @protocol BMFObjectParserProtocol <NSObject>
 
+@property (nonatomic, weak) id<BMFObjectParserDelegateProtocol> delegate;
+
+- (id) newObject;
+
+- (NSArray *) fetchAllLocalObjectsSortedById;
+
 - (NSComparisonResult) compareDictionary:(NSDictionary *) obj1 withDictionary:(NSDictionary *)obj2;
+- (NSComparisonResult) compareObject:(id) obj1 withObject:(id)obj2;
+- (NSComparisonResult) compareObject:(id) obj1 withDictionary:(id)obj2;
+
 - (BOOL)updateObject:(id) object withDictionary:(NSDictionary *)dictionary error:(NSError **) error;
 
+- (BOOL) deleteObject:(id) object;
+- (BOOL) deleteAllLocalObjects;
+
+- (BOOL) saveChanges;
+
 @end

bmf/shared/data/parsers/BMFParserStrategy.h

+//
+//  BMFParserStrategy.h
+//  metaBooksCoreData
+//
+//  Created by Jose Manuel Sánchez Peñarroja on 26/05/14.
+//  Copyright (c) 2014 Treenovum. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+
+#import <BMF/BMFObjectParserProtocol.h>
+
+@interface BMFParserStrategy : NSObject
+
+@property (nonatomic, assign) NSUInteger batchSize;
+
+- (NSArray *) parseDictionaries:(NSArray *) dictionaries localObjects:(NSArray *)localObjects objectParser:(id<BMFObjectParserProtocol>) objectParser;
+	
+@end

bmf/shared/data/parsers/BMFParserStrategy.m

+//
+//  BMFParserStrategy.m
+//  metaBooksCoreData
+//
+//  Created by Jose Manuel Sánchez Peñarroja on 26/05/14.
+//  Copyright (c) 2014 Treenovum. All rights reserved.
+//
+
+#import "BMFParserStrategy.h"
+
+#import <BMF/BMF.h>
+
+@implementation BMFParserStrategy
+
+- (instancetype) init {
+	self = [super init];
+	if (self) {
+		self.batchSize = 50;
+	}
+	return self;
+}
+
+- (NSArray *) parseDictionaries:(NSArray *) dictionaries localObjects:(NSArray *)localObjects objectParser:(id<BMFObjectParserProtocol>) objectParser{
+	BMFAbstractMethod();
+	return nil;
+}
+
+@end

bmf/shared/subspecs/coredata/BMFCompareParserStrategy.h

+//
+//  BMFCompareParserStrategy.h
+//  metaBooksCoreData
+//
+//  Created by Jose Manuel Sánchez Peñarroja on 26/05/14.
+//  Copyright (c) 2014 Treenovum. All rights reserved.
+//
+
+// 
+
+#import "BMFParserStrategy.h"
+
+@interface BMFCompareParserStrategy : BMFParserStrategy
+
+@end

bmf/shared/subspecs/coredata/BMFCompareParserStrategy.m

+//
+//  BMFCompareParserStrategy.m
+//  metaBooksCoreData
+//
+//  Created by Jose Manuel Sánchez Peñarroja on 26/05/14.
+//  Copyright (c) 2014 Treenovum. All rights reserved.
+//
+
+#import "BMFCompareParserStrategy.h"
+
+#import <BMF/BMF.h>
+
+@implementation BMFCompareParserStrategy
+
+- (NSArray *) parseDictionaries:(NSArray *) dictionaries localObjects:(NSArray *) localObjects objectParser:(id<BMFObjectParserProtocol>) objectParser {
+	
+	NSMutableArray *results = [NSMutableArray array];
+	
+	localObjects = [localObjects sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
+		return [objectParser compareObject:obj1 withObject:obj2];
+	}];
+	
+	NSArray *sortedDictionaries = [dictionaries sortedArrayUsingComparator:^NSComparisonResult(NSDictionary *obj1, NSDictionary *obj2) {
+		return [objectParser compareDictionary:obj1 withDictionary:obj2];
+	}];
+	
+	NSUInteger index = 0;
+	NSUInteger localIndex = 0;
+	NSUInteger serverIndex = 0;
+	
+	NSError *error = nil;
+	
+	while (localIndex<localObjects.count && serverIndex<sortedDictionaries.count) {
+		id localEntity = localObjects[localIndex];
+		
+		NSDictionary *serverEntityDic = sortedDictionaries[serverIndex];
+		
+		NSComparisonResult comparisonResult = [objectParser compareObject:localEntity withDictionary:serverEntityDic];
+		
+		if (comparisonResult==NSOrderedAscending) {
+			[objectParser deleteObject:localEntity];
+			
+			localIndex++;
+		}
+		else {
+			
+			id entity = localEntity;
+			
+			if (comparisonResult==NSOrderedDescending) {
+				// Add object
+				entity = nil;
+			}
+			else {
+				localIndex++;
+			}
+			
+			serverIndex++;
+			
+			
+			if (!entity) entity = [objectParser newObject];
+			
+			BOOL result = [objectParser updateObject:entity withDictionary:serverEntityDic error:&error];
+			if (!result) {
+				entity = nil;
+				DDLogError(@"Error updating object: %@",error);
+			}
+			
+			if (entity) {
+				[results addObject:entity];
+			}
+			else {
+				DDLogError(@"Error updating entity: %@",error);
+			}
+		}
+		
+		index++;
+		
+		if (index%self.batchSize==0) {
+			[objectParser saveChanges];
+		}
+	}
+	
+	// Delete all these objects
+	while (localIndex<localObjects.count) {
+		id localEntity = localObjects[localIndex];
+		[objectParser deleteObject:localEntity];
+		localIndex++;
+	}
+	
+	// Add all these objects
+	while (serverIndex<sortedDictionaries.count) {
+		
+		NSDictionary *serverEntityDic = sortedDictionaries[serverIndex];
+		
+		id entity = [objectParser newObject];
+		BOOL result = [objectParser updateObject:entity withDictionary:serverEntityDic error:&error];
+		if (!result) {
+			entity = nil;
+			DDLogError(@"Error updating object: %@",error);
+		}
+		else {
+			[results addObject:entity];
+		}
+		serverIndex++;
+	}
+	
+	return results;
+}
+
+@end

bmf/shared/subspecs/coredata/BMFCoreDataObjectParser.h

+//
+//  BMFCoreDataObjectParser.h
+//  Pods
+//
+//  Created by Jose Manuel Sánchez Peñarroja on 26/05/14.
+//
+//
+
+#import <Foundation/Foundation.h>
+
+#import <CoreData/CoreData.h>
+
+#import "BMFObjectParserProtocol.h"
+
+@interface BMFCoreDataObjectParser : NSObject <BMFObjectParserProtocol>
+
+@property (nonatomic, strong) NSManagedObjectContext *context;
+@property (nonatomic, weak) id<BMFObjectParserDelegateProtocol> delegate;
+
+- (instancetype) initWithContext:(NSManagedObjectContext *) context;
+
+@end

bmf/shared/subspecs/coredata/BMFCoreDataObjectParser.m

+//
+//  BMFCoreDataObjectParser.m
+//  Pods
+//
+//  Created by Jose Manuel Sánchez Peñarroja on 26/05/14.
+//
+//
+
+#import "BMFCoreDataObjectParser.h"
+
+#import "BMF.h"
+
+#import <MagicalRecord/CoreData+MagicalRecord.h>
+
+@implementation BMFCoreDataObjectParser
+
+- (instancetype) initWithContext:(NSManagedObjectContext *)context {
+	self = [super init];
+    if (self) {
+		self.context = context;
+	}
+    return self;
+}
+
+- (instancetype)init {
+	BMFInvalidInit(initWithContext:);
+}
+
+- (id) newObject {
+	BMFAbstractMethod();
+	return nil;
+}
+
+- (NSArray *) fetchAllLocalObjectsSortedById {
+	BMFAbstractMethod();
+	return nil;
+}
+
+- (NSComparisonResult) compareDictionary:(NSDictionary *) obj1 withDictionary:(NSDictionary *)obj2 {
+	BMFAbstractMethod();
+	return NSOrderedSame;
+}
+
+- (NSComparisonResult) compareObject:(NSManagedObject *)obj1 withDictionary:(NSDictionary *)obj2 {
+	BMFAbstractMethod();
+	return NSOrderedSame;
+}
+
+- (NSComparisonResult) compareObject:(NSManagedObject *)obj1 withObject:(NSManagedObject *)obj2 {
+	BMFAbstractMethod();
+	return NSOrderedSame;
+}
+
+- (BOOL) updateObject:(NSManagedObject *)object withDictionary:(NSDictionary *) dic error:(NSError **) error {
+	BMFAbstractMethod();
+	return NO;
+}
+
+- (BOOL) deleteObject:(NSManagedObject *)object {
+	[self.context deleteObject:object];
+	return YES;
+}
+
+- (BOOL) deleteAllLocalObjects {
+	BMFAbstractMethod();
+	return NO;
+}
+
+@end
+