Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 75 lines (60 sloc) 2.41 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
//
// RKSearchWordObserver.m
// RestKit
//
// Created by Blake Watters on 7/25/11.
// Copyright (c) 2009-2012 RestKit. All rights reserved.
//

#import <CoreData/CoreData.h>
#import "RKSearchWordObserver.h"
#import "RKSearchableManagedObject.h"
#import "RKLog.h"

// Set Logging Component
#undef RKLogComponent
#define RKLogComponent lcl_cRestKitCoreDataSearchEngine

static RKSearchWordObserver *sharedSearchWordObserver = nil;

@implementation RKSearchWordObserver

+ (RKSearchWordObserver *)sharedObserver
{
    if (! sharedSearchWordObserver) {
        sharedSearchWordObserver = [[RKSearchWordObserver alloc] init];
    }

    return sharedSearchWordObserver;
}

- (id)init
{
    self = [super init];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(managedObjectContextWillSaveNotification:)
                                                     name:NSManagedObjectContextWillSaveNotification
                                                   object:nil];
    }

    return self;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [super dealloc];
}

- (void)managedObjectContextWillSaveNotification:(NSNotification *)notification
{
    NSManagedObjectContext *context = [notification object];
    NSSet *candidateObjects = [[NSSet setWithSet:context.insertedObjects] setByAddingObjectsFromSet:context.updatedObjects];

    RKLogDebug(@"Managed object context will save notification received. Checking changed and inserted objects for searchable entities...");

    for (NSManagedObject *object in candidateObjects) {
        if (! [object isKindOfClass:[RKSearchableManagedObject class]]) {
            RKLogTrace(@"Skipping search words refresh for entity of type '%@': not searchable.", NSStringFromClass([object class]));
            continue;
        }

        NSArray *searchableAttributes = [[object class] searchableAttributes];
        for (NSString *attribute in searchableAttributes) {
            if ([[object changedValues] objectForKey:attribute]) {
                RKLogDebug(@"Detected change to searchable attribute '%@' for %@ entity: refreshing search words.", attribute, NSStringFromClass([object class]));
                [(RKSearchableManagedObject *)object refreshSearchWords];
                break;
            }
        }
    }
}

@end
Something went wrong with that request. Please try again.