Permalink
Browse files

Factored out map code

  • Loading branch information...
1 parent 0e6d661 commit 6f6b16335ec0d5a6375ec4b3b610411df26fb98d @chriseidhof committed Feb 14, 2012
@@ -35,6 +35,8 @@
8359A98714EA767600FBA59F /* Track.m in Sources */ = {isa = PBXBuildFile; fileRef = 8359A98614EA767600FBA59F /* Track.m */; };
8359A98C14EA97F400FBA59F /* BandCampAPI.m in Sources */ = {isa = PBXBuildFile; fileRef = 8359A98B14EA97F400FBA59F /* BandCampAPI.m */; };
8359A98D14EA97F400FBA59F /* BandCampAPI.m in Sources */ = {isa = PBXBuildFile; fileRef = 8359A98B14EA97F400FBA59F /* BandCampAPI.m */; };
+ 8359A99014EA98C000FBA59F /* NSArray+Map.m in Sources */ = {isa = PBXBuildFile; fileRef = 8359A98F14EA98C000FBA59F /* NSArray+Map.m */; };
+ 8359A99114EA98C000FBA59F /* NSArray+Map.m in Sources */ = {isa = PBXBuildFile; fileRef = 8359A98F14EA98C000FBA59F /* NSArray+Map.m */; };
/* End PBXBuildFile section */
/* Begin PBXContainerItemProxy section */
@@ -86,6 +88,8 @@
8359A98614EA767600FBA59F /* Track.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = Track.m; path = IncrementalStoreTest/Track.m; sourceTree = "<group>"; };
8359A98A14EA97F400FBA59F /* BandCampAPI.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BandCampAPI.h; sourceTree = "<group>"; };
8359A98B14EA97F400FBA59F /* BandCampAPI.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BandCampAPI.m; sourceTree = "<group>"; };
+ 8359A98E14EA98C000FBA59F /* NSArray+Map.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSArray+Map.h"; sourceTree = "<group>"; };
+ 8359A98F14EA98C000FBA59F /* NSArray+Map.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSArray+Map.m"; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@@ -166,6 +170,8 @@
8359A95114E9554C00FBA59F /* BandCampIS.m */,
8359A98A14EA97F400FBA59F /* BandCampAPI.h */,
8359A98B14EA97F400FBA59F /* BandCampAPI.m */,
+ 8359A98E14EA98C000FBA59F /* NSArray+Map.h */,
+ 8359A98F14EA98C000FBA59F /* NSArray+Map.m */,
);
path = IncrementalStoreTest;
sourceTree = "<group>";
@@ -336,6 +342,7 @@
8359A98314EA767600FBA59F /* Band.m in Sources */,
8359A98714EA767600FBA59F /* Track.m in Sources */,
8359A98C14EA97F400FBA59F /* BandCampAPI.m in Sources */,
+ 8359A99014EA98C000FBA59F /* NSArray+Map.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -345,6 +352,7 @@
files = (
8359A94714E9551200FBA59F /* IncrementalStoreTestTests.m in Sources */,
8359A98D14EA97F400FBA59F /* BandCampAPI.m in Sources */,
+ 8359A99114EA98C000FBA59F /* NSArray+Map.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -8,6 +8,7 @@
#import "BandCampIS.h"
#import "BandCampAPI.h"
+#import "NSArray+Map.h"
NSString *BANDCAMP_STORE_TYPE = @"BandCampIS";
@@ -65,7 +66,7 @@ - (id)executeRequest:(NSPersistentStoreRequest *)request withContext:(NSManagedO
}
}
- NSLog(@"unkonwn request: %@", request);
+ NSLog(@"unknown request: %@", request);
return nil;
}
@@ -85,24 +86,21 @@ - (NSArray *)obtainPermanentIDsForObjects:(NSArray *)array error:(NSError **)err
- (id)newValueForRelationship:(NSRelationshipDescription *)relationship forObjectWithID:(NSManagedObjectID *)objectID withContext:(NSManagedObjectContext *)context error:(NSError **)error {
if([relationship.entity.name isEqualToString:@"Band"]) {
if ([relationship.name isEqualToString:@"discography"]) {
- id bandId = [self referenceObjectForObjectID:objectID];
- NSMutableArray* results = [NSMutableArray array];
- for(NSDictionary* album in [BandCampAPI apiDiscographyForBandWithId:bandId]) {
- NSManagedObjectID* oid = [self objectIdForNewObjectOfEntity:relationship.destinationEntity nativeKey:@"album_id" values:album];
- [results addObject:oid];
- }
- return results;
+ id bandId = [self referenceObjectForObjectID:objectID];
+ NSArray* discographyData = [BandCampAPI apiDiscographyForBandWithId:bandId];
+ return [discographyData map:^(id album) {
+ return [self objectIdForNewObjectOfEntity:relationship.destinationEntity nativeKey:@"album_id" values:album];
+
+ }];
}
} else if([relationship.entity.name isEqualToString:@"Album"]) {
if([relationship.name isEqualToString:@"tracks"]) {
NSDictionary* values = [cache objectForKey:objectID];
NSArray* tracks = [values objectForKey:@"tracks"];
- NSMutableArray* results = [NSMutableArray array];
- for(NSDictionary* trackData in tracks) {
- NSManagedObjectID* oid = [self objectIdForNewObjectOfEntity:relationship.destinationEntity nativeKey:@"track_id" values:trackData];
- [results addObject:oid];
- }
- return results;
+ return [tracks map:^(id trackData){
+ return [self objectIdForNewObjectOfEntity:relationship.destinationEntity nativeKey:@"track_id" values:trackData];
+
+ }];
}
}
NSLog(@"relationship for unknown entity: %@", relationship.entity);
@@ -118,9 +116,4 @@ - (NSManagedObjectID*)objectIdForNewObjectOfEntity:(NSEntityDescription*)entityD
return oid;
}
-#pragma mark API methods
-
-
-
-
@end
@@ -0,0 +1,17 @@
+//
+// NSArray+Map.h
+// IncrementalStoreTest
+//
+// Created by Chris Eidhof on 2/14/12.
+// Copyright (c) 2012 Chris Eidhof. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+
+typedef id(^MapBlock)(id block);
+
+@interface NSArray (Map)
+
+- (NSArray*)map:(MapBlock)block;
+
+@end
@@ -0,0 +1,21 @@
+//
+// NSArray+Map.m
+// IncrementalStoreTest
+//
+// Created by Chris Eidhof on 2/14/12.
+// Copyright (c) 2012 Chris Eidhof. All rights reserved.
+//
+
+#import "NSArray+Map.h"
+
+@implementation NSArray (Map)
+
+- (NSArray*)map:(MapBlock)block {
+ NSMutableArray* result = [NSMutableArray arrayWithCapacity:self.count];
+ for(id item in self) {
+ [result addObject:block(item)];
+ }
+ return result;
+}
+
+@end

0 comments on commit 6f6b163

Please sign in to comment.