際際滷

際際滷Share a Scribd company logo
Of鍖ine Mode
Overcome your fear of implementing
in your apps
www.addof鍖inemode.com
Of鍖ine Mode
Overcome your fear of implementing
in your apps
www.addof鍖inemode.com
Marin Todorov
(me)
raywenderlich.com
Today you will
See common of鍖ine mode fails and wins
Draw a lot of good advice in the process
Finally have a look at few tools
Connected apps
web services
Internet in your pocket
Web services in the cloud
Photo credit: Flickr, Torkild RetvedtPhoto credit: Flickr, Tim Bates

Photo credit: Flickr, Torkild RetvedtPhoto credit: Flickr, Marcin Kargol
Oops (no connection)
Maybe?
Ehm?
of鍖ine gprs/edge
Awesome :)
Awesome :)
- of鍖ine
- bad connectivity
- online
3 modes
- of鍖ine
3 modes
- bad connectivity
3 modes
CTTelephonyNetworkInfo currentRadioAccessTechnology
CTRadioAccessTechnologyGPRS
CTRadioAccessTechnologyEdge
CTRadioAccessTechnologyLTE
CTRadioAccessTechnologyDidChangeNotification
- online
3 modes
current step
steps top pre-fetch
- rely on what makes sense
UI wise
How to handle of鍖ine
mode
There is no universal solution
THE END
- asses the problem
- evaluate the solution
- code
3 steps
in this order please!
Klout
Klout
NSUserDefaults
name
score
4 last updates
2 last connections
JSON
Klout
Online?
Start
Fetch JSON
UI Code
Done
Start
Fetch JSON
UI Code
Done
Local JSON
Store locally
YESNO
Simple solution for a simple problem
First asses, then evaluate, then code
York cinemas
York cinemas
cinemas in Berlin - these change once a year
!
鍖lm schedule changes every thursday
!
static information - practically doesnt change
!
ticket reservations
Photo credit: Flickr, Tobias Abel Photo credit: Flickr, InSapphoWeTrust
12 cinema photos + their names + coordinates
25 鍖lm posters + 鍖lm names
30 movie times for all cinemas
!
7KB sqlite 鍖le
2MB of photos
!
=
!
< 1 Facebook photo in size
20.03 Thu Sat 27.03 Thu
Check this out York cinemas!
This
week?
Start
Read sqlite database
UI Code
Done
Start
Fetch 1 鍖le
UI Code
Done
YES
NO
Read sqlite database
The problem de鍖nition contained the solution
Sqlite database is a single 鍖le
You dont need past data
How about ticket reservations?
You got to to some things online!
You can still do SOMETHING
A stock trading app
price changes all the time
!
displays historical data
!
allows for real-time buying/selling
price changes all the time
!
displays historical data
!
allows for real-time buying/selling
Past data does not change
14:40 14:50 15:00
14:30 14:40 14:50
14:40 14:50 15:00
Time on plane
14:50 15:00
Time on plane
BUY
3 hrs
For long lists - use sqlite
AAPL!
123456 11.6 10.5
123459 11.5 10.5
123460 12.0 11.0
123470 14.0 11.5
123490 14.0 10.5
Easy to sync to local database
server database JSON local sqlite
AAPL!
!
!
!
123470 14.0 11.5
123490 14.0 10.5
aapl
keep ids from the server
How to fetch the delta data?
14:40 14:50 15:00
+ tokenA + tokenB + tokenC
+ tokenA + tokenB
Facebook
posts come in all the time
!
posting in not time crucial
!
chat with friends
!
location check-in
14:40 14:50 13:20
Time on plane
Overcome your fear of implementing offline mode in your apps
14:40 14:50 13:20
Time on plane
OMG! Great 鍖ight
attendants on the
鍖ight to Lisbon!
Overcome your fear of implementing offline mode in your apps
Overcome your fear of implementing offline mode in your apps
Core Data = power + pain
You need to recreate the relationships
Migration strategy
Whats the foreseeable outcome of the create/edit
operations?
Data validation
Cache the create operation data
3 ways to store temporary objects
!
1. create the new objects, store them using NSCoding
2. create a separate store for pending objects
3. use existing store, but use ids < 0
How to change your app
architecture
cloud
Network layer
REST
JSON RPC
JSON API
XML? plist?
Connectivity
User Interface
data storage
NSCoding
sqlite3
Core Data
Cache
鍖le system
Core Data
鍖le storage
You probably have this setup
cloud
Network layer
REST
JSON RPC
JSON API
XML? plist?
Connectivity
User Interface
data storage
NSCoding
sqlite3
Core Data
Cache
鍖le system
Core Data
鍖le storage
You want to have this setup
Prefer reading from local source,
update local source from Internet
Power tools
network, storage, UI
sorry, iPhone only
CoreTelephony.framework
CTTelephonyNetworkInfo -> currentRadioAccessTechnology
!
CTTelephonyNetworkInfo -> subscriberCellularProvider.isoCountryCode
!
NSURLSession
NSURLSession
CloudCode Cookie policy
Auth policy
Caching policy
Storing 鍖les
Timeouts
etc. etc.
鰻皆雨檎晦皆艶壊壊庄看稼遺看稼鍖g顎姻温岳庄看稼
Reachability
https://github.com/tonymillion/Reachability
!
arc, gdc, ios/osx, maximum wins
reach	
 =	
 [Reachability	
 reachabilityWithHostname:@"www.addofflinemode.com"];	
 
reach.unreachableBlock	
 =	
 ^(Reachability*reach)	
 
{	
 
	
 	
 	
 	
 NSLog(@"UNREACHABLE!");	
 
};
JSONModel
https://github.com/icanzilb/JSONModel
!
model classes, validation, data conversion, etc.
JSONModel
{id:1, name:Marin}
NSNumber* id;
NSString* name;
NSManagedObject
Model
JSON
Core Data
NSFetchedResultsController
!
Core Data Editor
Image: http://thermal-core.com/CoreDataEditor/
Core Data
TBCoreDataStore!
!
http://robots.thoughtbot.com/core-data
Main Queue!
context
Background Queue!
context read
write
merge
Internet!
JSON
FMDB
https://github.com/ccgus/fmdb
!
Objc sqlite made easy
FMResultSet	
 *s	
 =	
 [db	
 executeQuery:@"SELECT	
 *	
 FROM	
 myTable"];	
 
while	
 ([s	
 next])	
 {	
 
	
 	
 	
 	
 //retrieve	
 values	
 for	
 each	
 record	
 
}
NSCoding
NSData* encodedData = [NSKeyedArchiver archivedDataWithRootObject: myObject];
[encodedData writeToURL:localFileURL atomically:YES];
Cache
simpler cache library - EGOCache
https://github.com/enormego/EGOCache
image caching - FastImageCache
https://github.com/path/FastImageCache
[[EGOCache globalCache] setObject:JSONResponse forKey:@"cachedJSON"];
[[EGOCache globalCache] setImage:myImage forKey:@"photo" 
withTimeoutInterval:60*60];
Sequencer
https://github.com/berzniz/Sequencer
!
async 鍖ow control
Get X Post Y Location Update UI
[sequencer enqueueStep:^(id result, SequencerCompletion completion) {
[self testSectionUnloggedGets: nil];
completion(nil);
}];
!
[sequencer enqueueStep:^(id result, SequencerCompletion completion) {
[self postToServer: ^{
completion(nil);
}];
}];
Whats next?
Turn on plane mode and check how your
favorite apps look like
Check how your app looks like (eeek!)
Get in touch, discuss, check out the power-tools
Thats all folks
Of鍖ine Mode
Overcome your fear of implementing
in your apps
www.addof鍖inemode.com

More Related Content

Overcome your fear of implementing offline mode in your apps