简体   繁体   中英

Best way to check existent data in the database (iOS)

I'm developing an app that manages messages, and I want the app connects to the server, get messages and save them in the database(coredata). If the messages already exist, doesnt do anything and if they dont, add them to the database.

I'm thinking some ways to do it but I don't know exactly what to do. Any help? Thanks in advance

I would recommend using Restkit framework http://restkit.org
Reskit provides integration with Core Data.

Pros of using Restkit:
- Combines HTTP request/responses API, along with object mapping, offline/caching support with Core Data, all in one framework
- Object Mapping means that you're writing clean code, you define your classes and how they map to the JSON attributes, then you GET/POST/DELETE with few lines of code after that
- Core Data support means that your projects can work offline, data is sync when working online, but persistent when you need it offline
- The framework is well maintained

Cons:
- Works only with JSON REST APIs
- There can be a steep learning curve for some aspects
- Can be challenging if you work with REST APIs that are not completely 'standard'

The simplest way is to add a guid attribute (an identifier of type NSString , for example) to the entity you are interested in and check for that guid when you import data.

Here, you have two ways: let the server generate the guid for you or implement your own algorithm in the client side (iPhone, iPad, etc.). In both cases you need to be sure the guid is unique for each message.

So, for example, suppose the server generates the messages (and each message has its own guid). When you import data you also save the guid for each message object. If you have already a message with a specific guid, you don't add it, otherwise you add it. This could be done using the Find-or-Create pattern (see Implementing Find-or-Create Efficiently ).

Hope that helps.

This is simple, it took me sometime to learn this, I use it in most of my apps.

First you need an ID of the fetched item, for example messageID. When you fetch the JSON with all the items, for example using AFNetworking, you're going to receive an array of objects in NSDictionaries.

Before parsing the item load all the IDs of your stored items in a NSMutableDictionary (key => messageID, value objectID, this is related to the Core Data fault).

Don't forget to init the NSMutableArray somewhere:

_dictionaryOfEventIDAndObjectID = [NSMutableDictionary dictionary];

- (void)prepareDictionaryOfMessageIDs
{
    [self.dictionaryOfEventIDAndObjectID removeAllObjects];

    NSError *error = nil;
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Message"];
    [fetchRequest setResultType:NSDictionaryResultType];
    NSExpressionDescription *objectIDDescription = [[NSExpressionDescription alloc] init];
    objectIDDescription.name = @"objectID";
    objectIDDescription.expression = [NSExpression expressionForEvaluatedObject];
    objectIDDescription.expressionResultType = NSObjectIDAttributeType;
    [fetchRequest setPropertiesToFetch:@[objectIDDescription, @"messageID"]];

    NSArray *objectsDict = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
    for (NSDictionary *objectDict in objectsDict) {
        [self.dictionaryOfMessageIDAndObjectID setObject:[objectDict valueForKeyPath:@"objectID"] forKey:[objectDict valueForKeyPath:@"messageID"]];
    }
}

Then in the fetched data completion block just add something like this:

    for (NSDictionary *objectDict in objectsDict) {

        NSString *fetchedID = [objectDict objectForKey:@"id"];

        if ([self.dictionaryOfMessageIDAndObjectID objectForKey:fetchedID]) {
            continue;
        }

        [self parseMessageFromDictionary:objectDict];
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM