2016-04-26 9 views

Antwort

-1

1.Database aktualisiert

Ich habe eine einzige Datenbank-Controller - VSDatabaseController in meinem neuesten app -, dass die Gespräche über FMDB SQLite. FMDB unterscheidet zwischen Updates und Abfragen. So aktualisieren Sie die Datenbank der App Anrufe:

-[VSDatabaseController runDatabaseBlockInTransaction:(VSDatabaseUpdateBlock)databaseBlock] 

VSDatabaseUpdateBlock ist einfach:

typedef void (^VSDatabaseUpdateBlock)(FMDatabase *database); 

runDatabaseBlockInTransaction ist auch einfach:

- (void)runDatabaseBlockInTransaction:(VSDatabaseUpdateBlock)databaseBlock { 
    dispatch_async(self.serialDispatchQueue, ^{ 
     @autoreleasepool { 
      [self beginTransaction]; 
      databaseBlock(self.database); 
      [self endTransaction]; 
     } 
    }); 
} 

Hier ist ein einfaches Beispiel für einen Aufruf der Datenbank zu aktualisieren: ALLE WÄHLEN

- (void)emptyTagsLookupTableForNote:(VSNote *)note { 
    NSString *uniqueID = note.uniqueID; 
    [self runDatabaseBlockInTransaction:^(FMDatabase *database) { 
     [database executeUpdate: 
      @"delete from tagsNotesLookup where noteUniqueID = ?;", uniqueID]; 
    }]; 
} 

[self.database executeUpdate: 
    @"CREATE INDEX if not exists noteUniqueIDIndex on tagsNotesLookup (noteUniqueID);"]; 
  1. Datenbank Fetching

    Objekte zu holen, die App Anrufe:

SELECT ALL

-[VSDatabaseController runFetchForClass:(Class)databaseObjectClass 
          fetchBlock:(VSDatabaseFetchBlock)fetchBlock 
         fetchResultsBlock:(VSDatabaseFetchResultsBlock)fetchResultsBlock]; 
These two lines do much of the work: 
SELECT ALL 
FMResultSet *resultSet = fetchBlock(self.database); 
NSArray *fetchedObjects = [self databaseObjectsWithResultSet:resultSet 
                 class:databaseObjectClass]; 

Eine Datenbank holen mit FMDB eine zurückgibt FMResultSet. Mit diesem resultSet können Sie Modellobjekte schrittweise durchlaufen und erstellen.

3.Keeping Objekte im Speicher

fmresultset * resultSet = [self.database executeQuery: @ "wählen uniqueID von some_table"];

4.Web APIs

- (void)uploadNote:(VSNote *)note { 
    VSNoteAPICall *apiCall = [[VSNoteAPICall alloc] initWithNote:[note detachedCopy]]; 
    [self enqueueAPICall:apiCall]; 
} 

5.Störungen Web API Rückgabewerte

VSNote *cachedNote = [self.mapTable objectForKey:downloadedNote.uniqueID]; 

6.Database Migration

[self.database executeUpdate:@"CREATE TABLE if not exists tags " 
    "(uniqueID TEXT UNIQUE, name TEXT, deleted INTEGER, deletedModificationDate DATE);"]; 

Ich hoffe, es wird Ihnen helfen.

1

Vielen Dank für Ihre Unterstützung Ich habe die Antwort gefunden: [Datenbank ExecuteUpdate: @ "Vakuum"];