2. DBACCESS IOS ORM
• [DBAccess] is a fully featured and FREE to use ORM for iOS.
• Replace CoreData whilst keeping your existing managed objects, but dump the
predicates and long-winded syntax.
• Instead use a simple and clean object syntax, with fast and concise inline
queries.
• DBAccess even has a conversion method to migrate your existing CoreData
tables across.
• Regularly updated and in constant use within many public applications it thrives
on feedback from other developers and is supported by the authors via
StackOverflow or directly via email.
• It's mantra is simple, to be fast, simple to implement and the first choice for any
3. WHY SHOULD I USE
[DBACCESS] ?
• An ORM should not be a chore to work with,
or require you to change your way of working
to compensate for its shortcomings. With
[DBAccess] you simply add it to your project
and start using it straight away.
4. WHY SHOULD I USE
[DBACCESS] ?
• The developer has full control over how the
ORM operates, deciding where it puts its
files, how queries are performed and on
which thread. Objects can be managed or
unmanaged, whilst being members of
domains which may share changes or be
isolated from them.
5. WHY SHOULD I USE
[DBACCESS] ?
• If memory is a concern, you can mix and
match lightweight objects to preserve system
resources when you are expecting large
result sets, or retrieve only the properties
you need with the remainder being lazily
loaded when accessed.
6. HEADLINE FEATURES
• Automatic modeling and upgrading from your class structures.
• Amazingly simple FLUENT interface for dealing with the ORM
• KILLER event model, for individual objects or tables. Makes coding apps a breeze
• Inline or Async queries
• Transaction support
• Managed and unmanaged objects supported to be used however you want
• Relationships mirror your class structures automatically, and all relationships are
automatically indexed
• Property level encryption so databases remain human readable whilst securing
individual columns.
13. CREATING DATA OBJECTS
• Person.m
#import "Person.h"
@implementation Person
@dynamic name, age, email, phone;
@end
14. CREATING OBJECTS,
SETTING VALUES &
PERSISTANCE-(void)createData
{
// Create a new object
Person *thisPerson = [Person new];
// Set some properties
thisPerson.age = 27;
thisPerson.email = @"happyboy@gmail.com";
thisPerson.name = @"HappyBoy";
thisPerson.phone = @"0987654321";
// Persist the object into the datastore
[thisPerson commit];
// Create a new object
Person *thatPerson = [Person new];
// Set some properties
thatPerson.age = 11;
thatPerson.email = @"happygirl@gmail.com";
thatPerson.name = @"HappyGirl";
thatPerson.phone = @"0912345678";
// Persist the object into the datastore
[thatPerson commit];
}
16. DBRESULTSET *RESULTS
真不敢相信可以直接在console列印出來!!!
-------------------------------------------------------------------------------------------
| Entity : Person Primary Key : Id Value: 25.000000 |
-------------------------------------------------------------------------------------------
| Field Name | Type | Value |
-------------------------------------------------------------------------------------------
| age | NUMBER | 11.000000 |
| email | TEXT | happygirl@gmail.com |
| phone | TEXT | 0912345678 |
| name | TEXT | HappyGirl |
| Id | NUMBER | 25.000000 |
-------------------------------------------------------------------------------------------
| Relationships |
-------------------------------------------------------------------------------------------
| Entity Name | Target Table | Status |
-------------------------------------------------------------------------------------------
| NONE | | |
-------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------
| Entity : Person Primary Key : Id Value: 24.000000 |
-------------------------------------------------------------------------------------------
| Field Name | Type | Value |
-------------------------------------------------------------------------------------------
| age | NUMBER | 27.000000 |
| email | TEXT | happyboy@gmail.com |
| phone | TEXT | 0987654321 |
| name | TEXT | HappyBoy |
| Id | NUMBER | 24.000000 |
-------------------------------------------------------------------------------------------
| Relationships |
-------------------------------------------------------------------------------------------
| Entity Name | Target Table | Status |
-------------------------------------------------------------------------------------------
| NONE | | |
-------------------------------------------------------------------------------------------
17. REMOVING OBJECTS
-(void)removeData
{
for (Person* person in [[Person query] fetch]) {
[person remove];
}
// or the shorthand is to use the removeAll method on the DBResultSet object
[[[Person query] fetch] removeAll];
}
18. OTHER TYPES OF QUERY
-(void)queryData
{
/* count the rows within the Person table */
int count = [[Person query] count];
/* add all of the ages together */
double total = [[Person query] sumOf:@"age"];
/* group all the people together by the surname property */
NSDictionary *peopleBySurname = [[Person query] groupBy:@"name"];
/* get just the primary keys for a query, useful to save memory */
NSArray *ids = [[Person query] ids];
}
22. 問題:撈回無法參照
• Status: Unloaded
-------------------------------------------------------------------------------------------
| Entity : Person Primary Key : Id Value: 5.000000 |
-------------------------------------------------------------------------------------------
| Field Name | Type | Value |
-------------------------------------------------------------------------------------------
| age | NUMBER | 27.000000 |
| name | UNKNOWN | Nil value |
| phone | UNKNOWN | Nil value |
| pet | NUMBER | 5.000000 |
| email | TEXT | happyboy@gmail.com |
| Id | NUMBER | 5.000000 |
-------------------------------------------------------------------------------------------
| Relationships |
-------------------------------------------------------------------------------------------
| Entity Name | Target Table | Status |
-------------------------------------------------------------------------------------------
| pet | Pet | Unloaded |
-------------------------------------------------------------------------------------------
29. 問題:無法繼承
• 編譯時沒事,執行時掛掉
• Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '***
setObjectForKey: key cannot be nil’
Superman *thisSuperman = [Superman new];
// Set some properties
thisSuperman.age = 27;
thisSuperman.email = @"QQQhappyboy@gmail.com";
thisSuperman.pet = thisPet;
thisSuperman.power = 99;
[thisSuperman commit];
30. QUERIES & RESULTS
• DEALING WITH DATA
• RETRIEVING OBJECTS
• WHERE CLAUSES
• JOIN
• ASYNC QUERIES
• THE RESULTS (AKA DBRESULTSET*)
• DIFFERENT TYPES OF QUERY (COUNT, SUM)
31. THE EVENT MODEL
• The event handler is probably DBAccess’s most useful
feature, it enables the developer to ‘hook’ into the events
raised by the database engine.
• There are 3 event types, INSERT, UPDATE & DELETE.
These are bitwise operators so can be combined together
to register blocks against multiple events at the same time.
• There are two DBEventHandler objects you can use, one
derived from the class object and another that can be used
from within an instance of a class.
33. TRANSACTIONS
• Transactions in DBAccess are handled using
a class method on DBTransaction,
transactions can be started from anywhere
at anytime, are entirely thread-safe. Any
modifications to records that are due to
event triggers being raised are also included
within the transaction and rolled back on
failure.