Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Externally exposing the lookupKey/relatedByAttribute #396

Closed
wants to merge 10 commits into from

2 participants

@stuffmc

Allows to use it like [[MyEntity entityDescription] lookupKey] — Maybe we could call this relatedByAttribute and move it to CoreData+MagicalRecord.h as a class method.

stuffmc added some commits
@stuffmc stuffmc Externally exposing the lookupKey to use it like `[[MyEntity entityDe…
…scription] lookupKey]` (Maybe we could call this `relatedByAttribute` and move it to `CoreData+MagicalRecord.h` as a class method.
1eda669
@stuffmc stuffmc Removed comments in MR_importFromArray + more close to coding style o…
…f the project.
5cad1dd
@stuffmc stuffmc Passing a ManagedObject to MR_importFromObject: allows to "duplicate"…
… that Object like with a NSDictionary. Skipping relationships in this case.
230b244
@tonyarnold
Owner

Looks good to me — thanks for fixing up the stylistic things, @stuffmc! We should remember to add MR_lookupKey to the shorthand header, too.

@stuffmc

Oh, wow. So this is the way Pull Requests works? Everytime I'll add something in my branch stuffmc you guys will get noticed? That's "interesting" — Now that I know that Maybe I should push to "my" experimental and when there are things I don't want to be merged here (or in dev for example).

@tonyarnold
Owner

Generally, the best approach is to create "feature branches". Basically, make discrete changes, create a new branch for each of them. Related changes are OK, but getting a whole swag of unrelated code to review is no fun.

Have a read of A successful Git branching model — this is how we have been working for the last couple of months with MR, and it's how we'd prefer to receive changes and pull requests.

That all said, any method of contribution is welcome and if there's merit in what you've done (even if it's a mega messy pull request) we might still cherry pick out the individual changes we want to merge.

@tonyarnold
Owner

Given the age of this issue, and the volume of issues we have to work through, I've decided to close this alongside a number of other older issues.

If you can still replicate the issue under the latest in-development version of MagicalRecord (3.0 at the time of writing), please feel free to re-open and one of @magicalpanda/team-magicalrecord will take another look. Thanks!

@tonyarnold tonyarnold closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 24, 2013
  1. @stuffmc

    Externally exposing the lookupKey to use it like `[[MyEntity entityDe…

    stuffmc authored
    …scription] lookupKey]` (Maybe we could call this `relatedByAttribute` and move it to `CoreData+MagicalRecord.h` as a class method.
  2. @stuffmc
Commits on Jan 26, 2013
  1. @stuffmc

    Passing a ManagedObject to MR_importFromObject: allows to "duplicate"…

    stuffmc authored
    … that Object like with a NSDictionary. Skipping relationships in this case.
  2. @stuffmc

    NSString Addition "MR_sortKeys" (and the corresponding define) allows…

    stuffmc authored
    … to use it in other contexts.
  3. @stuffmc

    Shorthand method for getting the numberOfRows of a NSFetchedResultCon…

    stuffmc authored
    …troller section without having to do the NSFetchedResultsSectionInfo Samba.
  4. @stuffmc
Commits on Jan 28, 2013
  1. @stuffmc

    Fixing a double @interface block

    stuffmc authored
  2. @stuffmc
  3. @stuffmc

    When an entity has a super entity, if no 'primaryAttribute' is found,…

    stuffmc authored
    … going upwards the hierarchy looking for one.
Commits on Jan 29, 2013
  1. @stuffmc
This page is out of date. Refresh to see the latest.
View
1  MagicalRecord/Categories/DataImport/NSEntityDescription+MagicalDataImport.h
@@ -11,5 +11,6 @@
- (NSAttributeDescription *) MR_primaryAttributeToRelateBy;
- (NSManagedObject *) MR_createInstanceInContext:(NSManagedObjectContext *)context;
+- (NSString*) MR_lookupKey;
@end
View
25 MagicalRecord/Categories/DataImport/NSEntityDescription+MagicalDataImport.m
@@ -12,12 +12,15 @@ @implementation NSEntityDescription (MagicalRecord_DataImport)
- (NSAttributeDescription *) MR_primaryAttributeToRelateBy;
{
- NSString *lookupKey = [[self userInfo] valueForKey:kMagicalRecordImportRelationshipLinkedByKey] ?: primaryKeyNameFromString([self name]);
- NSDictionary *attributesByName = [self attributesByName];
-
- if ([attributesByName count] == 0) return nil;
-
- NSAttributeDescription *primaryAttribute = [attributesByName objectForKey:lookupKey];
+ NSEntityDescription *entityDescription = self;
+ NSAttributeDescription *primaryAttribute = nil;
+ do {
+ NSDictionary *attributesByName = [entityDescription attributesByName];
+ if ([attributesByName count]) {
+ primaryAttribute = [attributesByName objectForKey:[entityDescription MR_lookupKey]];
+ };
+ entityDescription = entityDescription.superentity;
+ } while (!primaryAttribute && entityDescription);
return primaryAttribute;
}
@@ -30,5 +33,15 @@ - (NSManagedObject *) MR_createInstanceInContext:(NSManagedObjectContext *)conte
return newInstance;
}
+- (NSString*) MR_lookupKey
+{
+ NSString *primaryKeyName = [[self userInfo] valueForKey:kMagicalRecordImportRelationshipLinkedByKey] ?: primaryKeyNameFromString([self name]);
+ if ([[[self attributesByName] allKeys] containsObject:primaryKeyName]) {
+ return primaryKeyName;
+ } else {
+ NSString *primaryKeyNameInSuperentity = [[self superentity] MR_lookupKey];
+ return primaryKeyNameInSuperentity ?: primaryKeyName;
+ }
+}
@end
View
20 MagicalRecord/Categories/NSFetchedResultsController+MagicalRecord.h
@@ -0,0 +1,20 @@
+//
+// NSFetchedResultsController+MagicalRecord.h
+// wetter-com-iphone
+//
+// Created by Manuel "StuFF mc" Carrasco Molina on 26.01.13.
+// Copyright (c) 2013 grandcentrix GmbH. All rights reserved.
+//
+
+#import <CoreData/CoreData.h>
+
+#if TARGET_OS_IPHONE
+
+@interface NSFetchedResultsController (MagicalRecord)
+
+- (NSInteger)MR_numberOfObjectsInSection:(NSUInteger)section;
+- (NSUInteger)MR_fuzzyNumberOfObjectsInSection:(NSUInteger)section;
+
+@end
+
+#endif
View
34 MagicalRecord/Categories/NSFetchedResultsController+MagicalRecord.m
@@ -0,0 +1,34 @@
+//
+// NSFetchedResultsController+MagicalRecord.m
+// wetter-com-iphone
+//
+// Created by Manuel "StuFF mc" Carrasco Molina on 26.01.13.
+// Copyright (c) 2013 grandcentrix GmbH. All rights reserved.
+//
+
+#import "NSFetchedResultsController+MagicalRecord.h"
+
+#if TARGET_OS_IPHONE
+
+@implementation NSFetchedResultsController (MagicalRecord)
+
+- (NSInteger)MR_numberOfObjectsInSection:(NSUInteger)section
+{
+ NSUInteger count = -1; // This means section out of index in a nicer way!
+ if (section < self.sections.count) {
+ id<NSFetchedResultsSectionInfo> info = self.sections[section];
+ count = [info numberOfObjects];
+ }
+ return count;
+}
+
+- (NSUInteger)MR_fuzzyNumberOfObjectsInSection:(NSUInteger)section
+{
+ NSInteger count = [self MR_numberOfObjectsInSection:section];
+ count = (count > 0) ? count : 0;
+ return count;
+}
+
+@end
+
+#endif
View
31 MagicalRecord/Categories/NSManagedObject/NSManagedObject+MagicalDataImport.m
@@ -44,7 +44,7 @@ - (void) MR_setAttributes:(NSDictionary *)attributes forKeysWithObject:(id)objec
for (NSString *attributeName in attributes)
{
NSAttributeDescription *attributeInfo = [attributes valueForKey:attributeName];
- NSString *lookupKeyPath = [objectData MR_lookupKeyForAttribute:attributeInfo];
+ NSString *lookupKeyPath = [objectData isKindOfClass:[self class]] ? attributeName : [objectData MR_lookupKeyForAttribute:attributeInfo];
if (lookupKeyPath)
{
@@ -205,8 +205,10 @@ - (BOOL) MR_performDataImportFromObject:(id)objectData relationshipBlock:(void(^
NSDictionary *attributes = [[self entity] attributesByName];
[self MR_setAttributes:attributes forKeysWithObject:objectData];
- NSDictionary *relationships = [[self entity] relationshipsByName];
- [self MR_setRelationships:relationships forKeysWithObject:objectData withBlock:relationshipBlock];
+ if (![objectData isKindOfClass:[self class]]) { // Not importing Relationships when the object is a ManagedObject — I was otherwise in an infinite loop.
+ NSDictionary *relationships = [[self entity] relationshipsByName];
+ [self MR_setRelationships:relationships forKeysWithObject:objectData withBlock:relationshipBlock];
+ }
return [self MR_postImport:objectData];
}
@@ -234,7 +236,7 @@ + (id) MR_importFromObject:(id)objectData inContext:(NSManagedObjectContext *)co
{
NSAttributeDescription *primaryAttribute = [[self MR_entityDescription] MR_primaryAttributeToRelateBy];
- id value = [objectData MR_valueForAttribute:primaryAttribute];
+ id value = [objectData isKindOfClass:self] ? [objectData valueForKey:[[self MR_entityDescription] MR_lookupKey]] : [objectData MR_valueForAttribute:primaryAttribute];
NSManagedObject *managedObject = [self MR_findFirstByAttribute:[primaryAttribute name] withValue:value inContext:context];
if (managedObject == nil)
@@ -259,28 +261,7 @@ + (NSArray *) MR_importFromArray:(NSArray *)listOfObjectData
+ (NSArray *) MR_importFromArray:(NSArray *)listOfObjectData inContext:(NSManagedObjectContext *)context
{
-// NSMutableArray *objectIDs = [NSMutableArray array];
-//
-// [MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext)
-// {
-// [listOfObjectData enumerateObjectsWithOptions:0 usingBlock:^(id obj, NSUInteger idx, BOOL *stop)
-// {
-// NSDictionary *objectData = (NSDictionary *)obj;
-//
-// NSManagedObject *dataObject = [self MR_importFromObject:objectData inContext:localContext];
-//
-// if ([context obtainPermanentIDsForObjects:[NSArray arrayWithObject:dataObject] error:nil])
-// {
-// [objectIDs addObject:[dataObject objectID]];
-// }
-// }];
-// }];
-//
-// return [self MR_findAllWithPredicate:[NSPredicate predicateWithFormat:@"self IN %@", objectIDs] inContext:context];
-
-
// See https://gist.github.com/4501089 and https://alpha.app.net/tonymillion/post/2397422
-
NSMutableArray *objects = [NSMutableArray array];
[listOfObjectData enumerateObjectsWithOptions:0
View
3  MagicalRecord/Categories/NSManagedObject/NSManagedObject+MagicalFinders.m
@@ -102,7 +102,8 @@ + (id) MR_findFirst
}
+ (id) MR_findFirstByAttribute:(NSString *)attribute withValue:(id)searchValue inContext:(NSManagedObjectContext *)context
-{
+{
+ NSAssert(attribute != nil, @"attribute can't be nil — You either need to use 'relatedByAttribute' or use 'entityID'...");
NSFetchRequest *request = [self MR_requestFirstByAttribute:attribute withValue:searchValue inContext:context];
// [request setPropertiesToFetch:[NSArray arrayWithObject:attribute]];
View
2  MagicalRecord/Categories/NSManagedObject/NSManagedObject+MagicalRequests.m
@@ -115,7 +115,7 @@ + (NSFetchRequest *) MR_requestAllSortedBy:(NSString *)sortTerm ascending:(BOOL)
[request setFetchBatchSize:[self MR_defaultBatchSize]];
NSMutableArray* sortDescriptors = [[NSMutableArray alloc] init];
- NSArray* sortKeys = [sortTerm componentsSeparatedByString:@","];
+ NSArray* sortKeys = [sortTerm MR_sortKeys];
for (NSString* sortKey in sortKeys)
{
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:sortKey ascending:ascending];
View
16 MagicalRecord/Categories/NSString+MagicalRecord.h
@@ -0,0 +1,16 @@
+//
+// NSString+MagicalRecord.h
+// wetter-com-iphone
+//
+// Created by Manuel "StuFF mc" Carrasco Molina on 26.01.13.
+// Copyright (c) 2013 grandcentrix GmbH. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+
+@interface NSString (MagicalRecord)
+
+#define MR_SORT_KEY_SEPARATOR @","
+@property (strong, nonatomic, readonly) NSArray *MR_sortKeys;
+
+@end
View
18 MagicalRecord/Categories/NSString+MagicalRecord.m
@@ -0,0 +1,18 @@
+//
+// NSString+MagicalRecord.m
+// wetter-com-iphone
+//
+// Created by Manuel "StuFF mc" Carrasco Molina on 26.01.13.
+// Copyright (c) 2013 grandcentrix GmbH. All rights reserved.
+//
+
+#import "NSString+MagicalRecord.h"
+
+@implementation NSString (MagicalRecord)
+
+- (NSArray*)MR_sortKeys
+{
+ return [self componentsSeparatedByString:MR_SORT_KEY_SEPARATOR];
+}
+
+@end
View
16 MagicalRecord/Core/MagicalRecordShorthand.h
@@ -1,11 +1,5 @@
#ifdef MR_SHORTHAND
-
-
-
-
-
-
@interface NSManagedObject (MagicalAggregationShortHand)
+ (NSNumber *) numberOfEntities;
+ (NSNumber *) numberOfEntitiesWithContext:(NSManagedObjectContext *)context;
@@ -184,11 +178,11 @@
- (void) addiCloudContainerID:(NSString *)containerID contentNameKey:(NSString *)contentNameKey localStoreNamed:(NSString *)localStoreName cloudStorePathComponent:(NSString *)subPathComponent;
- (void) addiCloudContainerID:(NSString *)containerID contentNameKey:(NSString *)contentNameKey localStoreNamed:(NSString *)localStoreName cloudStorePathComponent:(NSString *)subPathComponent completion:(void(^)(void))completionBlock;
@end
-
-
-
-
-
+@interface NSEntityDescription (MagicalRecordShortHand)
+- (NSAttributeDescription *) primaryAttributeToRelateBy;
+- (NSManagedObject *) createInstanceInContext:(NSManagedObjectContext *)context;
+- (NSInteger)numberOfObjectsInSection:(NSUInteger)section;
+@end
#endif
View
3  MagicalRecord/CoreData+MagicalRecord.h
@@ -40,10 +40,13 @@
#import "NSNumber+MagicalDataImport.h"
#import "NSObject+MagicalDataImport.h"
#import "NSString+MagicalDataImport.h"
+ #import "NSString+MagicalRecord.h"
#import "NSAttributeDescription+MagicalDataImport.h"
#import "NSRelationshipDescription+MagicalDataImport.h"
#import "NSEntityDescription+MagicalDataImport.h"
+ #import "NSFetchedResultsController+MagicalRecord.h"
+
#endif
// @see https://github.com/ccgus/fmdb/commit/aef763eeb64e6fa654e7d121f1df4c16a98d9f4f
Something went wrong with that request. Please try again.