Java自学者论坛

 找回密码
 立即注册

手机号码,快捷登录

恭喜Java自学者论坛(https://www.javazxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,会员资料板块,购买链接:点击进入购买VIP会员

JAVA高级面试进阶训练营视频教程

Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程Go语言视频零基础入门到精通Java架构师3期(课件+源码)
Java开发全终端实战租房项目视频教程SpringBoot2.X入门到高级使用教程大数据培训第六期全套视频教程深度学习(CNN RNN GAN)算法原理Java亿级流量电商系统视频教程
互联网架构师视频教程年薪50万Spark2.0从入门到精通年薪50万!人工智能学习路线教程年薪50万大数据入门到精通学习路线年薪50万机器学习入门到精通教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程MySQL入门到精通教程
查看: 20570|回复: 0

IOS多线程读写Sqlite问题解决

[复制链接]
  • TA的每日心情
    奋斗
    2024-4-6 11:05
  • 签到天数: 748 天

    [LV.9]以坛为家II

    2034

    主题

    2092

    帖子

    70万

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

    积分
    705612
    发表于 2021-4-6 16:56:57 | 显示全部楼层 |阅读模式

    现在ios里使用的数据库一般都是Sqlite,但是使用Sqlite有个不太好的地方就是在多线程的时候,会出现问题,sqlite只能打开一个读或者写连结。这样的话多线程就会碰到资源占用的问题。

     

    最开始是使用FMDB,FMDB的早期版本不能解决这个问题,后来FMDB更新了,新版本的FMDB能够很好的解决这个多线程使用Sqlite 。

    FMDB github网址  https://github.com/ccgus/fmdb 最新版的请到github取下载。

     

    本文演示了使用FMDB通过多线程来读和写数据库操作。

    1.建立数据库表,我采用的是Firefox的Sqlite manager 来建立的。

      建表sql如下

     CREATE TABLE "tbl_user" ("_id" INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL , "name" VARCHAR(30), "password" VARCHAR(30))

     

     2. 建立数据表的映射实体UserEntity

    #import <Foundation/Foundation.h>

    @interface UserEntity : NSObject
    {
         int _id;
        NSString *name;
        NSString *password;
    }

    @property (nonatomic, assign) int ID;
    @property (nonatomic, retain) NSString *name;
    @property (nonatomic, retain) NSString *password;

    @end 

     

    3. 建立操作数据库的dao

    //
    //   DbDao.m
    //   SqliteTest
    //
    //   Created by foxwang on 12-4-9.
    //   Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
    //

    #import  " DbDao.h "
    #import  " DbFileManager.h "

    #import  " FMDatabase.h "
    #import  " FMDatabaseAdditions.h "
    #import  " FMDatabasePool.h "
    #import  " FMDatabaseQueue.h "
    #import  " UserEntity.h "

    static DbDao *gSharedInstance = nil;

    @implementation DbDao
    @synthesize dbFile;
    @synthesize dbQueue;

    +(DbDao *)sharedInstance
    {
        @synchronized(self)
        {
             if (gSharedInstance == nil)
                gSharedInstance = [[DbDao alloc] init];
        }
         return gSharedInstance;    
    }

    - ( void)dealloc
    {
        [self.dbFile release];
        self.dbQueue = nil;
        [super dealloc];
    }

    - ( id)init
    {
        
        self = [super init];
         if (self)
        {
            self.dbFile = [DbFileManager dbFilePath];
            self.dbQueue = [FMDatabaseQueue databaseQueueWithPath:self.dbFile];
            
            
        }
         return  self;
    }


    - (UserEntity *)rsToUser:(FMResultSet*)rs
    {
        UserEntity *user = [[[UserEntity alloc] init] autorelease];
        user.ID = [rs intForColumn: @" _id "];
        user.name = [rs stringForColumn: @" name "];
        user.password = [rs  stringForColumn: @" password "];
         return user;
    }

    - ( void)addUser:(UserEntity *)user
    {
        [self.dbQueue inTransaction:^(FMDatabase *db, BOOL *rollback) {
            [db open];
            NSString *sql =  @" insert into tbl_user(name, password) values (?, ?) ";
            [db executeUpdate:sql,user.name, user.password];
            [db close];
        }];  
    }

    - (NSArray *)getUsers;
    {
        __block NSMutableArray *users = [[[NSMutableArray alloc] init] autorelease];  
        [self.dbQueue inDatabase:^(FMDatabase *db)   {
            [db open];
            NSString *sql =  @" select * from tbl_user  ";
            FMResultSet *rs = [db executeQuery:sql];
             while ([rs next])
            {
                [users addObject:[self rsToUser :rs]]; 
            }
            [db close];
        }];
         return users;
    }

    @end 

     

    4. 编写测试方法

    在didFinishLaunchingWithOptions 方法里启动3个线程 :2个线程写,1个线程读

     - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

    {
        self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
         //  Override point for customization after application launch.
        self.viewController = [[[ViewController alloc] initWithNibName: @" ViewController " bundle:nil] autorelease];
        self.window.rootViewController = self.viewController;
        [self.window makeKeyAndVisible];
        
        
        [NSThread detachNewThreadSelector:@selector(writeDbOne) toTarget:self withObject:nil];
        
        [NSThread detachNewThreadSelector:@selector(readDb) toTarget:self withObject:nil];
        
        [NSThread detachNewThreadSelector:@selector(writeDbTwo) toTarget:self withObject:nil];
        
         return YES;
    }

     

    - ( void)writeDbOne
    {
        DbDao *dao = [DbDao  sharedInstance];
         for ( int i =  0; i <  500; i++)
        {
            @autoreleasepool 
            {
                 UserEntity *user = [[[UserEntity alloc] init] autorelease];
                 user.name = [NSString stringWithFormat: @" name %d ", i];
                 user.password = [NSString stringWithFormat: @" password %d ", i];
                 [dao addUser:user];
                 NSLog( @" writeDbOne %d  ", i);
            }
           
        }
    }

    - ( void)writeDbTwo
    {
        DbDao *dao = [DbDao  sharedInstance];
         for ( int i =  600; i <  1200; i++)
        {
            @autoreleasepool 
            {
                UserEntity *user = [[[UserEntity alloc] init] autorelease];
                user.name = [NSString stringWithFormat: @" name %d ", i];
                user.password = [NSString stringWithFormat: @" password %d ", i];
                [dao addUser:user];
                NSLog( @" writeDbTwo %d  ", i);
            }
            
        }
    }

    - ( void)readDb
    {
         DbDao *dao = [DbDao  sharedInstance];
         NSArray *users =   [dao getUsers];
         NSLog( @" %@ ", users);

     

    最后查看数据库信息,数据成功插入

     

     

     

    结论 :使用新的FMDB ,很好的解决了多线程问题。 

     

     项目文件下载

     

    团结就是力量,ios开发者自己的推广联盟 QQ群173063969  

     

    哎...今天够累的,签到来了1...
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|小黑屋|Java自学者论坛 ( 声明:本站文章及资料整理自互联网,用于Java自学者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-5-16 22:57 , Processed in 0.067374 second(s), 29 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表