0

听起来很愚蠢,但我应该SQLITE_THREADSAFE在我的代码中设置哪里。Sqlite我的应用程序中的数据库用于我异步访问的多个实例。因此,我想让数据库线程安全。其中一项声明如下:

- (void)CreateNotification:(NSMutableArray*)arrNotification{

    for(Notification *notification in arrNotification)
    {
        sqlite3_stmt *statement;

        @try
        {
            @synchronized(self)
            {
                const char *sql = "INSERT INTO Notification (NotificationId, CreatedOn, EventName, GroupName, Message, NotificationBy, NotificationName, NotificationByDefaultPicture, Type, IsRead, NotificationByName, ChildName, ActionId, EventId, UserEmailId, UserRole, GroupId, InvitationId, InvitedUserName, InvitedEventUserRole, InvitedEventInvitationId, InvitedEventUserIsVerified, InvitedEventUserName, MessageId)VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

                if (sqlite3_prepare_v2(database, sql, -1, &statement, NULL) == SQLITE_OK)
                {
                    sqlite3_bind_text(statement, 1, [[notification NotificationId] UTF8String], -1, SQLITE_TRANSIENT);
                    sqlite3_bind_text(statement, 2, [[notification CreatedOn] UTF8String], -1, SQLITE_TRANSIENT);
                    sqlite3_bind_text(statement, 3, [[notification EventName] UTF8String], -1, SQLITE_TRANSIENT);
                    sqlite3_bind_text(statement, 4, [[notification GroupName] UTF8String], -1, SQLITE_TRANSIENT);
                    sqlite3_bind_text(statement, 5, [[notification Message] UTF8String], -1, SQLITE_TRANSIENT);
                    sqlite3_bind_text(statement, 6, [[notification NotificationBy] UTF8String], -1, SQLITE_TRANSIENT);
                    sqlite3_bind_text(statement, 7, [[notification NotificationName] UTF8String], -1, SQLITE_TRANSIENT);
                    sqlite3_bind_text(statement, 8, [[notification NotificationByDefaultPicture] UTF8String], -1, SQLITE_TRANSIENT);
                    sqlite3_bind_int(statement, 9, [[notification Type] intValue]);
                    sqlite3_bind_int(statement, 10, [[notification IsRead] intValue]);
                    sqlite3_bind_text(statement, 11, [[notification NotificationByName] UTF8String], -1, SQLITE_TRANSIENT);
                    sqlite3_bind_text(statement, 12, [[notification ChildName] UTF8String], -1, SQLITE_TRANSIENT);
                    sqlite3_bind_text(statement, 13, [[notification ActionId] UTF8String], -1, SQLITE_TRANSIENT);
                    sqlite3_bind_text(statement, 14, [[notification EventId] UTF8String], -1, SQLITE_TRANSIENT);
                    sqlite3_bind_text(statement, 15, [[notification UserEmailId] UTF8String], -1, SQLITE_TRANSIENT);
                    sqlite3_bind_int(statement, 16, [[notification UserRole] intValue]);
                    sqlite3_bind_text(statement, 17, [[notification GroupId] UTF8String], -1, SQLITE_TRANSIENT);
                    sqlite3_bind_text(statement, 18, [[notification InvitationId] UTF8String], -1, SQLITE_TRANSIENT);
                    sqlite3_bind_text(statement, 19, [[notification InvitedUserName] UTF8String], -1, SQLITE_TRANSIENT);
                    sqlite3_bind_int(statement, 20, [[notification InvitedEventUserRole] intValue]);
                    sqlite3_bind_text(statement, 21, [[notification InvitedEventInvitationId] UTF8String], -1, SQLITE_TRANSIENT);
                    sqlite3_bind_int(statement, 22, [[notification InvitedEventUserIsVerified] intValue]);
                    sqlite3_bind_text(statement, 23, [[notification InvitedEventUserName] UTF8String], -1, SQLITE_TRANSIENT);
                    sqlite3_bind_text(statement, 24, [[notification MessageId] UTF8String], -1, SQLITE_TRANSIENT);

                    if (sqlite3_step(statement) != SQLITE_DONE) {
                        NSLog(@"Insert failed: %s", sqlite3_errmsg(database));
                    }
                    else
                    {
                        NSLog(@"Notification inserted successfully !!");
                    }
                }
                else
                {
                    NSLog(@"Prepare-error #%i: %s", sqlite3_prepare_v2(database, sql, -1, &statement, NULL), sqlite3_errmsg(database));
                }
            }
        }
        @catch (NSException *exception) {

            NSLog(@"Error in CreateActivities : %@", exception.description);
        }
        @finally {

            sqlite3_finalize(statement);
        }
    }
}
4

0 回答 0