DBTransaction

//  Created by  张国锋 on 15-7-23.
//  Copyright (c) 2014年  张国锋. All rights reserved.
//

#import "AppDelegate.h"
#import "RootViewController.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    RootViewController *root = [[RootViewController alloc] init];
    self.window.rootViewController =root;
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}



//
//  RootViewController.m
//  DBTransaction  -FMDB
//
//  Created by  张国锋 on 15-7-23.
//  Copyright (c) 2014年  张国锋. All rights reserved.
//

#import "RootViewController.h"
#import "FMDatabase.h"
//需要导入libsqlite3.dylib系统库
@interface RootViewController ()
{
    FMDatabase *_dataBase;
}
@end

@implementation RootViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSString *dbPath = [NSHomeDirectory() stringByAppendingFormat:@"/Documents/test.db"];
    //初始化
    _dataBase = [[FMDatabase alloc] initWithPath:dbPath];
    if ([_dataBase open]) {
       //创建表
        NSString *createSql = @"create table if not exists student(id integer,name varchar(256))";
        if (![_dataBase executeUpdate:createSql]) {
            NSLog(@"create error:%@",_dataBase.lastErrorMessage);
        }
    }
    //NSDate 时间类
    NSDate *date1 = [NSDate date];//获取系统当前时间
    [self insertDataWithCount:1000 isUseTransaction:YES];
    NSDate *date2 = [NSDate date];
    //取到时间的差值  (timeIntervalSinceDate 两个时间的差值,单位是秒)
    //NSTimeInterval 时间差变量,秒
    NSTimeInterval time = [date2 timeIntervalSinceDate:date1];
    NSLog(@"time:%f",time);
    
	// Do any additional setup after loading the view.
}
//插入批量数据,是否手动启用事务
- (void)insertDataWithCount:(NSInteger)count isUseTransaction:(BOOL)isUse{
    if (isUse) {
        //手动启用事务
        BOOL isError = NO;
        @try {
         //写可能出现异常的代码
            [_dataBase beginTransaction];//手动开启一个事务
            for (int i=0; i<count; i++) {
                NSString *idStr =[NSString stringWithFormat:@"%d",i];
                NSString *stName = [NSString stringWithFormat:@"student%d",i];
                NSString *insertSql = @"insert into student(id,name) values(?,?)";
                if (![_dataBase executeUpdate:insertSql,idStr,stName]) {
                    NSLog(@"insert error:%@",_dataBase.lastErrorMessage);
                }
            }
        }
        @catch (NSException *exception) {
          //捕获到异常
            NSLog(@"error:%@",exception.reason);
            isError = YES;
            [_dataBase rollback];//回滚,回到最初的状态
        }
        @finally {
           //无论有没有异常,代码都会执行到此处
            if (isError==NO) {
                [_dataBase commit];//提交事务,让批量操作生效
                
                 
            }
        }
    }else{
       //常规操作
        for (int i=0; i<count; i++) {
            NSString *idStr =[NSString stringWithFormat:@"%d",i];
            NSString *stName = [NSString stringWithFormat:@"student%d",i];
            NSString *insertSql = @"insert into student(id,name) values(?,?)";
            if (![_dataBase executeUpdate:insertSql,idStr,stName]) {
                NSLog(@"insert error:%@",_dataBase.lastErrorMessage);
            }
        }
    }
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

 

//清除缓存

[[SDImageCache sharedImageCache]getSize]


{
[[SDImageCache sharedImageCache] clearDisk];
    [[SDImageCache sharedImageCache] clearMemory];
 [[NSURLCache sharedURLCache] removeAllCachedResponses];
    //清除cookies
    NSHTTPCookie *cookie;
    NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    for (cookie in [storage cookies])  {
        [storage deleteCookie:cookie];
    }
}

+ (void)cancelWebCache
{
    [[NSURLCache sharedURLCache] removeAllCachedResponses];
    //清除cookies
    NSHTTPCookie *cookie;
    NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    for (cookie in [storage cookies])  {
        [storage deleteCookie:cookie];
    }
}

 

转载于:https://www.cnblogs.com/0515offer/p/4695949.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值