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
MVC-N Discussion
Search
Marcus S. Zarra
March 10, 2016
Technology
8
47k
MVC-N Discussion
A discussion about proper networking in your iOS based application
Marcus S. Zarra
March 10, 2016
Tweet
Share
Other Decks in Technology
See All in Technology
Engineer Career Talk
lycorp_recruit_jp
0
190
ドメインの本質を掴む / Get the essence of the domain
sinsoku
2
160
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
0
110
Incident Response Practices: Waroom's Features and Future Challenges
rrreeeyyy
0
160
【令和最新版】AWS Direct Connectと愉快なGWたちのおさらい
minorun365
PRO
5
760
OCI Vault 概要
oracle4engineer
PRO
0
9.7k
CDCL による厳密解法を採用した MILP ソルバー
imai448
3
160
BLADE: An Attempt to Automate Penetration Testing Using Autonomous AI Agents
bbrbbq
0
320
Security-JAWS【第35回】勉強会クラウドにおけるマルウェアやコンテンツ改ざんへの対策
4su_para
0
180
リンクアンドモチベーション ソフトウェアエンジニア向け紹介資料 / Introduction to Link and Motivation for Software Engineers
lmi
4
300k
Exadata Database Service on Dedicated Infrastructure(ExaDB-D) UI スクリーン・キャプチャ集
oracle4engineer
PRO
2
3.2k
iOSチームとAndroidチームでブランチ運用が違ったので整理してます
sansantech
PRO
0
150
Featured
See All Featured
StorybookのUI Testing Handbookを読んだ
zakiyama
27
5.3k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
33
1.9k
Done Done
chrislema
181
16k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
10
720
Why You Should Never Use an ORM
jnunemaker
PRO
54
9.1k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
229
52k
What's in a price? How to price your products and services
michaelherold
243
12k
How To Stay Up To Date on Web Technology
chriscoyier
788
250k
Visualization
eitanlees
145
15k
A Tale of Four Properties
chriscoyier
156
23k
Building an army of robots
kneath
302
43k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9.1k
Transcript
Marcus S. Zarra MVC-N
MyUIViewController Network Request Network Request OtherUIViewController Network Request Network Request
ThirdUIViewController Network Request Network Request Controller Layer FourthUIViewController Network Request Network Request Core Data Anti-Pattern
MVCN Network Controller
MVCN MyUIViewController OtherUIViewController ThirdUIViewController FourthUIViewController NetworkController NR NR NR NR
NR NR Core Data The Net
NetworkController NR NR NR NR NR NR Network Controller Dependency
Injection Singleton AppDelegate property class NetworkController: NSObject { } let queue = NSOperationQueue() var mainContext: NSManagedObjectContext? func requestMyData() func requestMyData() -> NSFetchedResultsController func requestMyData(completion: (Void) -> Bool)
NetworkController NR NR NR NR NR NR Network Controller class
MyNetworkRequest: NSOperation { var context: NSManagedObjectContext? init(myParam1: String, myParam2: String) }
class MyNetworkRequest: NSOperation { var context: NSManagedObjectContext? private var innerContext:
NSManagedObjectContext? private var task: NSURLSessionTask? private let incomingData = NSMutableData() init(myParam1: String, myParam2: String) { super.init() //set some parameters here } var internalFinished: Bool = false override var finished: Bool { get { return internalFinished } set (newAnswer) { willChangeValueForKey("isFinished") internalFinished = newAnswer didChangeValueForKey("isFinished") } } }
func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveResponse response: NSURLResponse, completionHandler: (NSURLSessionResponseDisposition)
-> Void) { if cancelled { finished = true sessionTask?.cancel() return } //Check the response code and react appropriately completionHandler(.Allow) } func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveData data: NSData) { if cancelled { finished = true sessionTask?.cancel() return } incomingData.appendData(data) }
func URLSession(session: NSURLSession, task: NSURLSessionTask, didCompleteWithError error: NSError?) { if
cancelled { finished = true sessionTask?.cancel() return } if error != nil { self.error = error log("Failed to receive response: \(error)") finished = true return } //PROCESS DATA INTO CORE DATA finished = true }
Unit Testing
Network Conditions • Operations can report speeds • Control Performance
via the Queue • Network Controller can react to conditions • Central location for canceling of actions • Single decision point for background processing
Wrap up • Own your network code • Don’t put
network code in view controllers • Keep all network code together • All UI should feed from Core Data • Disconnect data collection and data display
Marcus S. Zarra
[email protected]
http://www.cimgf.com Thank You