【iOS】【FMDB】【sqlite】try catch实现事务处理

批量操作数据库的时候,我们经常会用到事务处理。事务是访问并可能更新数据库各种数据项的数据单元(unit),是恢复和并发的控制的基本单位。

事务具有四个属性。
- 原子性(atomicity):一个事务是一个不可分割的工作单位,事务中包括的诸操作要么都做,要么都不做。
- 一致性(consistency):事务必须是使数据库从一个一致性状态变到另一个一致性状态。一致性与原子性是密切相关的。
- 隔离性(isolation):一个事务的执行不能被其他事务干扰。即一个事务内部的操作及使用的数据对并发的其他事务是隔离的,并发执行的各个事务之间不能互相干扰。
- 持久性(durability):持续性也称永久性(permanence),指一个事务一旦提交,它对数据库中数据的改变就应该是永久性的。接下来的其他操作或故障不应该对其有任何影响。

sqlite支持事务,FMDB也对事务进行了很好的封装。

事务形成的sql语句
触发事务:begin transaction
提交事务:commit transaction
回滚事务:rollback transaction

【FMDB】中关于事务的封装如下:(看看实现方式,一般我们只用到方法)

#pragma mark Transactions

- (BOOL)rollback {   
    BOOL b = [self executeUpdate:@"rollback transaction"];

    if (b) {
        _inTransaction = NO;
    }

    return b;
}

- (BOOL)commit {
    BOOL b =  [self executeUpdate:@"commit transaction"];

    if (b) {
        _inTransaction = NO;
    }

    return b;
}

- (BOOL)beginDeferredTransaction {

    BOOL b = [self executeUpdate:@"begin deferred transaction"];
    if (b) {
        _inTransaction = YES;
    }

    return b;
}

- (BOOL)beginTransaction {

    BOOL b = [self executeUpdate:@"begin exclusive transaction"];
    if (b) {
        _inTransaction = YES;
    }

    return b;
}

- (BOOL)inTransaction {
    return _inTransaction;
}

在工程里的使用

AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
[delegate.db  beginTransaction];
        BOOL isRollBack = NO;
        @try {
            /** 开始下载数据 */
            [self downLoadData];
        }
        @catch (NSException *exception) {
            isRollBack = YES;
            [delegate.db  rollback];
        }
        @finally {
            if (!isRollBack) {
                [delegate.db  commit];
            }
        }

-downLoadData;方法是实现的数据获取及插入,这里不写了。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
Title: iOS 8 App Development Essentials Author: Neil Smyth Length: 824 pages Edition: 1 Language: English Publisher: CreateSpace Independent Publishing Platform Publication Date: 2014-12-16 ISBN-10: 1505586410 ISBN-13: 9781505586411 iOS 8 App Development Essentials is the latest edition of this popular book series and has now been fully updated for the Swift programming language, the iOS 8 SDK and Xcode 6. Beginning with the basics, this book provides an outline of the steps necessary to set up an iOS development environment. An introduction to the architecture of iOS 8 and programming in Swift is provided, followed by an in-depth look at the design of iOS applications and user interfaces. More advanced topics such as file handling, database management, in-app purchases, graphics drawing and animation are also covered, as are touch screen handling, gesture recognition, multitasking, iAds integration, location management, local notifications, camera access and video and audio playback support. Other features are also covered including Auto Layout, Twitter and Facebook integration, App Store hosted in-app purchase content, collection views, Sprite Kit-based game development, local map search and user interface animation using UIKit dynamics. The key new features of the iOS 8 SDK and Xcode 6 are also covered, including Swift playgrounds, universal user interface design using size classes, app extensions, Interface Builder Live Views, embedded frameworks, CloudKit data storage and TouchID authentication. The aim of this book is to teach the range of skills necessary to build apps for iOS 8. iOS 8 App Development Essentials takes a modular approach to the subject of iOS 8 application development for both the iPhone and iPad, with each chapter covering a self contained topic area consisting of detailed explanations, examples and step-by-step tutorials. This makes the book both an easy to follow learning aid and an excellent reference resource. Table of Contents Chapter 1. Start Here Chapter 2. Joining the Apple iOS Developer Program Chapter 3. Installing Xcode 6 and the iOS 8 SDK Chapter 4. A Guided Tour of Xcode 6 Chapter 5. Testing Apps on iOS 8 Devices with Xcode 6 Chapter 6. An Introduction to Swift Playgrounds Chapter 7. Swift Data Types, Constants and Variables Chapter 8. Swift Operators and Expressions Chapter 9. Swift Flow Control Chapter 10. The Swift Switch Statement Chapter 11. An Overview of Swift Functions and Closures Chapter 12. The Basics of Object Oriented Programming in Swift Chapter 13. An Introduction to Swift Inheritance Chapter 14. Working with Array and Dictionary Collections in Swift Chapter 15. The iOS 8 Application and Development Architecture Chapter 16. Creating an Interactive iOS 8 App Chapter 17. Understanding iOS 8 Views, Windows and the View Hierarchy Chapter 18. An Introduction to Auto Layout in iOS 8 Chapter 19. Working with iOS 8 Auto Layout Constraints in Interface Builder Chapter 20. An iOS 8 Auto Layout Example Chapter 21. Implementing iOS 8 Auto Layout Constraints in Code Chapter 22. Implementing Cross-Hierarchy Auto Layout Constraints in iOS 8 Chapter 23. Understanding the iOS 8 Auto Layout Visual Format Language Chapter 24. Using Size Classes to Design Universal iOS User Interfaces Chapter 25. Using Storyboards in Xcode 6 Chapter 26. Using Xcode 6 Storyboards to Create an iOS 8 Tab Bar Application Chapter 27. An Overview of iOS 8 Table Views and Xcode 6 Storyboards Chapter 28. Using Xcode 6 Storyboards to Build Dynamic TableViews with Prototype Table View Cells Chapter 29. Implementing iOS 8 TableView Navigation using Storyboards in Xcode 6 Chapter 30. An iOS 8 Split View Master-Detail Example Chapter 31. Implementing a Page based iOS 8 Application using UIPageViewController Chapter 32. An Example iOS 8 UIPageViewController Application Chapter 33. Working with Directories in Swift on iOS 8 Chapter 34. Working with Files in Swift on iOS 8 Chapter 35. iOS 8 Directory Handling and File I/O in Swift – A Worked Example Chapter 36. Preparing an iOS 8 App to use iCloud Storage Chapter 37. Managing Files using the iOS 8 UIDocument Class Chapter 38. Using iCloud Storage in an iOS 8 Application Chapter 39. Synchronizing iOS 8 Key-Value Data using iCloud Chapter 40. iOS 8 Data Persistence using Archiving Chapter 41. iOS 8 Database Implementation using SQLite Chapter 42. An Example SQLite based iOS 8 Application using Swift and FMDB Chapter 43. Working with iOS 8 Databases using Core Data Chapter 44. An iOS 8 Core Data Tutorial Chapter 45. An Introduction to CloudKit Data Storage on iOS 8 Chapter 46. An iOS 8 CloudKit Example Chapter 47. An iOS 8 CloudKit Subscription Example Chapter 48. An Overview of iOS 8 Multitouch, Taps and Gestures Chapter 49. An Example iOS 8 Touch, Multitouch and Tap Application Chapter 50. Detecting iOS 8 Touch Screen Gesture Motions Chapter 51. Identifying Gestures using iOS 8 Gesture Recognizers Chapter 52. An iOS 8 Gesture Recognition Tutorial Chapter 53. Implementing TouchID Authentication in iOS 8 Apps Chapter 54. An Overview of iOS 8 Collection View and Flow Layout Chapter 55. An iOS 8 Storyboard-based Collection View Tutorial Chapter 56. Subclassing and Extending the iOS 8 Collection View Flow Layout Chapter 57. Drawing iOS 8 2D Graphics with Core Graphics Chapter 58. Interface Builder Live Views and iOS 8 Embedded Frameworks Chapter 59. An iOS 8 Graphics Tutorial using Core Graphics and Core Image Chapter 60. Basic iOS 8 Animation using Core Animation Chapter 61. iOS 8 UIKit Dynamics – An Overview Chapter 62. An iOS 8 UIKit Dynamics Tutorial Chapter 63. An Introduction to iOS 8 Sprite Kit Programming Chapter 64. An iOS 8 Sprite Kit Level Editor Game Tutorial Chapter 65. An iOS 8 Sprite Kit Collision Handling Tutorial Chapter 66. An iOS 8 Sprite Kit Particle Emitter Tutorial Chapter 67. Integrating iAds into an iOS 8 App Chapter 68. iOS 8 Multitasking, Background Transfer Service and Fetching Chapter 69. Scheduling iOS 8 Local Notifications Chapter 70. An Overview of iOS 8 Application State Preservation and Restoration Chapter 71. An iOS 8 State Preservation and Restoration Tutorial Chapter 72. Integrating Maps into iOS 8 Applications using MKMapItem Chapter 73. An Example iOS 8 MKMapItem Application Chapter 74. Getting Location Information using the iOS 8 Core Location Framework Chapter 75. An Example iOS 8 Location Application Chapter 76. Working with Maps on iOS 8 with MapKit and the MKMapView Class Chapter 77. Working with MapKit Local Search in iOS 8 Chapter 78. Using MKDirections to get iOS 8 Map Directions and Routes Chapter 79. An Introduction to Extensions in iOS 8 Chapter 80. An iOS 8 Today Extension Widget Tutorial Chapter 81. Creating an iOS 8 Photo Editing Extension Chapter 82. Creating an iOS 8 Action Extension Chapter 83. Receiving Data from an iOS 8 Action Extension Chapter 84. Using iOS 8 Event Kit to Create Date and Location Based Reminders Chapter 85. Accessing the iOS 8 Camera and Photo Library Chapter 86. An Example iOS 8 Camera Application Chapter 87. iOS 8 Video Playback using AVPlayer and AVPlayerViewController Chapter 88. Playing Audio on iOS 8 using AVAudioPlayer Chapter 89. Recording Audio on iOS 8 with AVAudioRecorder Chapter 90. Integrating Twitter and Facebook into iOS 8 Applications Chapter 91. An iOS 8 Facebook Integration Tutorial using UIActivityViewController Chapter 92. iOS 8 Facebook and Twitter Integration using SLRequest Chapter 93. An iOS 8 Twitter Integration Tutorial using SLRequest Chapter 94. Making Store Purchases with the SKStoreProductViewController Class Chapter 95. Building In-App Purchasing into iOS 8 Applications Chapter 96. Preparing an iOS 8 Application for In-App Purchases Chapter 97. An iOS 8 In-App Purchase Tutorial Chapter 98. Configuring and Creating App Store Hosted Content for iOS 8 In-App Purchases Chapter 99. Preparing and Submitting an iOS 8 Application to the App Store

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值