AlexDenisov / iActiveRecord

ActiveRecord for iOS without CoreData, only SQLite
http://alexdenisov.github.com/iActiveRecord/
MIT License
354 stars 50 forks source link

Cannot find ignore_fields_do in the code base? #68

Open Fykec opened 10 years ago

Fykec commented 10 years ago

only find the validations function

AlexDenisov commented 10 years ago

Wiki info outdated, this fields removed. Current version uses @dynamic

Fykec commented 10 years ago

but I really want to ignore some field, How can i do this?

AlexDenisov commented 10 years ago

You should create your model and mark properties as @dynamic, other properties will be ignored.

@interface User : ActiveRecord

@property (nonatomic, copy) NSString *email;
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *password; // will be ignored

@end

@implementation

@dynamic email;
@dynamic name;

@end

This way only name and email will be used by AR.

Fykec commented 10 years ago

Hi Alex

Thank you, I know it. for example

I have a json with key

name, images, 

but I want the object have

name and images(not to save), imageURLStr(one image from images)

but if I want to

RestKit mapping images(json) to images(object)

override the method imageURLStr like bellow

- (NSString *)imageURLStr
{
    if (imageURLStr == nil)
    {
        if (images && [images count] > 0)
        {
            return [images objectAtIndex:0];
        }
    }

    return imageURLStr;
}

and

ignore images

the @dynamic will not let me do this, and If I use @synthesize, the code will not work, so I cannot to find to good way to override when use @dynamic

Then, I have to

mapping images to imageURLStr

by a transformer

RKValueTransformer *arrayToStringTransfromer = [RKBlockValueTransformer valueTransformerWithValidationBlock:^BOOL(__unsafe_unretained Class inputValueClass, __unsafe_unretained Class outputValueClass) {
    DebugLog(@"[%@|%@|%d] %@", NSStringFromClass([self class]) , NSStringFromSelector(_cmd) , __LINE__ ,@"");
    return [inputValueClass isSubclassOfClass:[NSArray class]] && [outputValueClass isSubclassOfClass:[NSString class]];
} transformationBlock:^BOOL(id inputValue, __autoreleasing id *outputValue, __unsafe_unretained Class outputClass, NSError *__autoreleasing *error) {

    if ([inputValue count] > 0)
    {
        *outputValue = [[inputValue objectAtIndex:0] copy];
    }

    return YES;
}];

RKAttributeMapping *imagesMapping = [RKAttributeMapping attributeMappingFromKeyPath:@"images" toKeyPath:@"imageURLStr"];
imagesMapping.valueTransformer = arrayToStringTransfromer;

and

not do tricky things in AR