Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
SBJson 4: Don't make me wait!
Search
Stig Brautaset
March 05, 2014
Programming
0
410
SBJson 4: Don't make me wait!
How SBJson 4 can help you get happy users.
Stig Brautaset
March 05, 2014
Tweet
Share
More Decks by Stig Brautaset
See All by Stig Brautaset
Delighting your users with SBJson 4
stig
1
1.3k
Other Decks in Programming
See All in Programming
CSC509 Lecture 04
javiergs
PRO
0
300
Pythonスレッドとは結局何なのか? CPython実装から見るNoGIL時代の変化
curekoshimizu
5
1.4k
CSC305 Lecture 05
javiergs
PRO
0
210
LLMとPlaywright/reg-suitを活用した jQueryリファクタリングの実際
kinocoboy2
4
670
After go func(): Goroutines Through a Beginner’s Eye
97vaibhav
0
240
あなたの知らない「動画広告」の世界 - iOSDC Japan 2025
ukitaka
0
410
Go言語の特性を活かした公式MCP SDKの設計
hond0413
1
200
私はどうやって技術力を上げたのか
yusukebe
43
17k
CI_CD「健康診断」のススメ。現場でのボトルネック特定から、健康診断を通じた組織的な改善手法
teamlab
PRO
0
180
Pull-Requestの内容を1クリックで動作確認可能にするワークフロー
natmark
2
470
止められない医療アプリ、そっと Swift 6 へ
medley
1
130
CSC509 Lecture 03
javiergs
PRO
0
330
Featured
See All Featured
Building Applications with DynamoDB
mza
96
6.6k
Optimising Largest Contentful Paint
csswizardry
37
3.4k
The Language of Interfaces
destraynor
162
25k
GraphQLの誤解/rethinking-graphql
sonatard
73
11k
Bash Introduction
62gerente
615
210k
KATA
mclloyd
32
15k
Build your cross-platform service in a week with App Engine
jlugia
232
18k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
29
2.6k
Designing Experiences People Love
moore
142
24k
Being A Developer After 40
akosma
91
590k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
32
2.2k
Side Projects
sachag
455
43k
Transcript
SBJson 4: Don’t make me wait! Stig Brautaset ~ @stigbra
Who am I? Started SBJson in 2007. Compulsively tinkered with
it since.
Fun facts 7 years on • 1012 commits • 4
major releases • 30 contributors • 3247 “stars” • 688 “forks”
–Every non-trivial app, ever. “Download a list of records &
display them to the user.”
What’s more important to your users? • Time until they
see the last record? • Or until they see the first?
The trouble with pagination • Repeated connection overhead • Records
fall in the cracks between pages • Records appear on two consecutive pages
Download-then-parse Time to first record Time to last record
Chunked delivery Time to first record Time to last record
Multi-core chunked delivery Time to first record Time to last
record
Relative time to first record 0 1.25 2.5 3.75 5
1 record 2 records 3 records 4 records 5 records Chunked delivery Traditional
NSJSONSerialisation • Supports NSStream! • But can’t do chunked delivery.
• We can do better!
SBJson 3 • Chunked delivery since June 2011 :-) •
But it was confusing and cumbersome to use… :-/ • …requiring multiple delegates chained together :-(
SBJson 4 • Drops category & OO interfaces • Focus
on chunked delivery • Introduces a block-based interface • Built specifically for two primary use cases
Multi-root JSON [1,2,3] {“foo”:42} [true,false]
Multi-root JSON id parser = [SBJson4Parser multiRootParserWithBlock:block errorHandler:eh];
Multi-root JSON [parser parse:data("[]{")]; // calls block(@[]) [parser parse:data("}[]"]; //
next part of stream // calls block(@{}) // calls block(@[])
Multi-root JSON [parser parse:data("[]{")]; // calls block(@[]) [parser parse:data("}[]"]; //
next part of stream // calls block(@{}) // calls block(@[])
Compromise: “garbage after JSON” []**xx block(@[]) errorHandler(…)
–Johnny Appleseed “What if I am using a 3rd-party API
that just gives me a long list?”
Root array chunks [ [1,2,3], {“foo”:42}, [true,false] ]
Unwrap the root array! id parser = [SBJson4Parser unwrapRootArrayParserWithBlock:block errorHandler:eh];
Unwrap the root array! [parser parse:data("[[],{")]; // calls block(@[]) [parser
parse:data("},true]")]; // calls block(@{}) // calls block(@(YES))
Unwrap the root array! [parser parse:data("[[],{")]; // calls block(@[]) [parser
parse:data("},true]")]; // calls block(@{}) // calls block(@(YES))
Compromise: Corrupted / incomplete JSON [ {}, [], ***###$#”>P block(@{})
block(@[]) errorHandler(…)
Unwrapping is for root-level arrays only!
SBJson’s Blocks SBJson4ValueBlock block = ^(id obj, BOOL *stop) {
if (some_condition) { *stop = YES; } else { // do something with obj } };
SBJson4ParserStatus switch ([parser parse:data]) { case SBJson4ParserStopped: case SBJson4ParserComplete: NSLog(“Done!");
case SBJson4ParserWaitingForData: break; case SBJson4ParserError: NSLog(@"Ouch, I'm hurt!"); break; }
Use with custom NSURLSessionDataDelegate - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dt didReceiveData:(NSData
*)data { switch ([parser parse: data]) { // handle various returns here… } }
Two CocoaPods! To allow both version 3 and 4 in
the same application.
http://www.flickr.com/photos/dullhunk/202872717/