Giter Site home page Giter Site logo

bldatabase's Introduction

BLDatabase

  1. This ORM data access
  2. Database changes reflect to NSIndexPath that will refresh UI automatically
  3. Read write concurrency
  4. You could preload a few properties, when you access others properties than load from disk
  5. LRU objects cache
  6. Based on FMDB

Usage

There six main classes in BLDatabase

  1. BLBaseDBObject Represents table and attributes
  2. BLDatabase Represents database. Used for creating data connections
  3. BLDatabaseConnection Used for creating transaction
  4. BLDatabaseTransaction Used for insert, update, delete, find objects
  5. BLFetchedResultsController Used for refreshing UI automatically
  6. BLFetchRequest Represents sql

Create database model

Create a class extend from BLBaseDBObject

#import "BLBaseDBObject.h"

BL_ARRAY_TYPE(BLAccount);

@interface BLAccount : BLBaseDBObject

@property (nonatomic, copy) NSString *accessToken;
@property (nonatomic, copy) NSString *userId;
@property (nonatomic, copy) NSString *nickname;

@property (nonatomic, strong) BLAccount *relationship;  // one to one
@property (nonatomic, copy) NSString *relationshipId;   // name must be [relationshipname] + [Id]

@property (nonatomic, strong) NSArray<BLAccount> *relationships; // one to many
@property (nonatomic, copy) NSArray *relationshipsIds;           // name must be [relationshipname] + [Ids]

@end

Create table and migration

self.database = [BLDatabase defaultDatabase];
[self.database setSchemaVersion:1
             withMigrationBlock:^(BLDatabaseReadWriteTransaction *transaction, NSUInteger oldSchemaVersion) {
                  [BLTestObject createTableAndIndexIfNeededInTransaction:transaction];
                  [BLAccount createTableAndIndexIfNeededInTransaction:transaction];
             }];

Executing find

// async find
BLDatabaseConnection *connection = [self.database connection];
[connection asyncReadWriteTransaction:^(BLDatabaseReadWriteTransaction *readWriteTransaction, BOOL *rollback) {
    NSArray *result = [BLTestObject findObjectsInTransaction:readWriteTransaction
                                                 withRequest:nil];
}];

// sync find
BLDatabaseConnection *connection = [self.database connection];
[connection syncReadWriteTransaction:^(BLDatabaseReadWriteTransaction *readWriteTransaction, BOOL *rollback) {
    NSArray *result = [BLTestObject findObjectsInTransaction:readWriteTransaction
                                                 withRequest:nil];
}];

Executing insert update delete

// async transaction
BLDatabaseConnection *connection = [database connection];
[connection asyncReadWriteTransaction:^(BLDatabaseReadWriteTransaction *readWriteTransaction, BOOL *rollback) {
  //...
}];

// sync transaction
BLDatabaseConnection *connection = [database connection];
[connection syncReadWriteTransaction:^(BLDatabaseReadWriteTransaction *readWriteTransaction, BOOL *rollback) {
  //...
}];

BLDatabaseConnection *connection = [database connection];
[connection syncReadWriteTransaction:^(BLDatabaseReadWriteTransaction *readWriteTransaction, BOOL *rollback) {
      // insert
      BLTestObject *object = [BLTestObject new];
      object.age = 20;
      object.name = @"test";
      [readWriteTransaction insertObject:object];
      
      // update
      object.age = 30;
      [readWriteTransaction updateObject:object];
      
      // delete
      [readWriteTransaction deleteObject:object];
}];

Relationship

// one to one
BLDatabaseConnection *connection = [database connection];
[connection syncReadWriteTransaction:^(BLDatabaseReadWriteTransaction *readWriteTransaction, BOOL *rollback) {
    BLAccount *account = [BLAccount new];
    NSString *uniqueId = account.uniqueId;
    BLAccount *account1 = [BLAccount new];
    NSString *uniqueId1 = account1.uniqueId;
    account.relationship = account1;
    account1.relationship = account;
    [readWriteTransaction insertObjects:@[account, account1]];
}];

// one to many
BLDatabaseConnection *connection = [database connection];
[connection syncReadWriteTransaction:^(BLDatabaseReadWriteTransaction *readWriteTransaction, BOOL *rollback) {
    BLAccount *account = [BLAccount new];
    NSString *uniqueId = account.uniqueId;
    
    BLAccount *account1 = [BLAccount new];
    NSString *uniqueId1 = account1.uniqueId;
    
    BLAccount *account2 = [BLAccount new];
    NSString *uniqueId2 = account2.uniqueId;
    
    account.relationships = (NSArray<BLAccount> *)@[account1, account2];
    [readWriteTransaction insertObjects:@[account, account1, account2]];
}];

BLFetchedResultsController

// create 
BLFetchRequest *request = [BLFetchRequest new];
request.sqlAfterWhere = nil;
request.fieldNames = @[@"name", @"groupName"];
request.sortTerm = @"name:1";
BLDatabaseConnection *connection = [database connection];

BLFetchedResultsController *controller = [[BLFetchedResultsController alloc] initWithFetchRequest:request
                                                                                   groupByKeyPath:@"groupName"
                                                                                   groupAscending:YES
                                                                                      objectClass:[BLTestObject class]
                                                                                     inConnection:connection];
controller.delegate = self;
[controller performFetch];
[self.tableView reloadData];

// refresh ui
- (void)controller:(BLFetchedResultsController *)controller didChangeWithIndexSet:(NSIndexSet *)indexSet forChangeType:(BLFetchedResultsChangeType)type
{
    if (type == BLFetchedResultsChangeInsert) {
        [self.tableView insertSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
    } else if (type == BLFetchedResultsChangeDelete) {
        [self.tableView deleteSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
    }
}

- (void)controller:(BLFetchedResultsController *)controller didChangeWithIndexPaths:(NSArray *)indexPaths forChangeType:(BLFetchedResultsChangeType)type
{
    if (type == BLFetchedResultsChangeInsert) {
        [self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationLeft];
    } else if (type == BLFetchedResultsChangeUpdate) {
        [self.tableView reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
    } else if (type == BLFetchedResultsChangeDelete) {
        [self.tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationRight];
    }
}

- (void)controllerWillChangeContent:(BLFetchedResultsController *)controller
{
    [self.tableView beginUpdates];
}

- (void)controllerDidChangeContent:(BLFetchedResultsController *)controller
{
    [self.tableView endUpdates];
}

bldatabase's People

Contributors

surewxw avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.