Tuesday, February 7, 2023
Learning Code
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#
No Result
View All Result
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#
No Result
View All Result
Learning Code
No Result
View All Result
Home Swift

My favorite new Swift API from iOS 15 – Hacking with Swift

learningcode_x1mckf by learningcode_x1mckf
September 11, 2022
in Swift
0
My favorite new Swift API from iOS 15 – Hacking with Swift
74
SHARES
1.2k
VIEWS
Share on FacebookShare on Twitter



You might also like

The abstract Vapor service factory design pattern

SwiftNIO tutorial – The echo server

Introducing – Vapor cheatsheet – The.Swift.Dev.

WWDC21 got here filled with plenty of new options for Swift, SwiftUI, Basis, and extra, however in all of the WWDC movies I’ve watched just one made me do a double take – I needed to rewind and rewatch it simply to ensure I hadn’t misheard.

The characteristic sounds easy, however makes for fairly astonishing code: it’s the strains property on URL, which downloads and returns strains of textual content from a URL as they’re fetched. Internally this builds on an enormous quantity of performance, not least effectful read-only properties and AsyncSequence, and the tip result’s fairly outstanding when it comes to its simplicity.

In its easiest type this implies we will entry string information instantly from a URL, both native or distant. That is excellent for easier APIs that vend information line by line, similar to plain-text recordsdata or CSV – Swift even retains the connection open so long as obligatory so new information may be streamed in as wanted.

Sponsor Hacking with Swift and reach the world’s largest Swift community!

Making an attempt it out with SwiftUI

Utilizing this API, we may write a small app to fetch a group of quotes from a server and show them in a listing, all utilizing hardly any code:

struct ContentView: View {
    @State non-public var quotes = [String]()

    var physique: some View 
        Record(quotes, id: .self, rowContent: Textual content.init)
            .job 
                do 
                    let url = URL(string: "https://hws.dev/quotes.txt")!

                    for strive await quote in url.strains 
                        quotes.append(quote)
                    
                 catch 
                    // Cease including quotes when an error is thrown
                
        
    
}

Maybe you possibly can see what made me do the double take within the first place – I used to be stunned to have the ability to entry strains instantly on a URL, fairly than routing by URLSession or comparable. Even higher, if a number of strains arrive without delay, @State is wise sufficient to batch its view reloads to keep away from creating additional work.

Parsing a CSV with AsyncSequence

When working with information in CSV format, you possibly can cut up your information up by commas then assign them to properties in no matter information sort you’re utilizing. That is made specifically handy due to AsyncSequence offering a compactMap() technique that transforms parts as they arrive, so we will switch a CSV line right into a Swift struct utilizing a customized initializer like this:

struct Consumer: Identifiable 
    let id: Int
    let firstName: String
    let lastName: String
    let nation: String

    init?(csv: String) 
        let fields = csv.elements(separatedBy: ",")
        guard fields.depend == 4 else  return nil 
        self.id = Int(fields[0]) ?? 0
        self.firstName = fields[1]
        self.lastName = fields[2]
        self.nation = fields[3]
    


struct ContentView: View {
    @State non-public var customers = [User]()

    var physique: some View 
        Record(customers)  person in
            VStack(alignment: .main) 
                Textual content("(person.firstName) (person.lastName)")
                    .font(.headline)
                Textual content(person.nation)
            
        
        .job 
            do 
                let url = URL(string: "https://hws.dev/customers.csv")!
                let userData = url.strains.compactMap(Consumer.init)

                for strive await person in userData 
                    customers.append(person)
                
             catch 
                // Cease including customers when an error is thrown
            
        
    
}

The facility of compactMap() is that it returns a brand new AsyncSequence that routinely applies a change because the strains arrive – it received’t drive us to await the info earlier than reworking all the things.

Streaming in information

Probably the most highly effective options of URL.strains is that its underlying AsyncSequence will hold the distant connection open for so long as it takes for all the info to reach. This implies your server can ship preliminary outcomes as quickly as they’re out there, then ship increasingly over time, till ultimately the request is full.

Necessary: Once you’re utilizing URL.strains on this approach, the system implements a 16KB buffer in order that your outcomes get batched up. This stops it from spinning over a loop for 1000’s of tiny strains, but additionally means generally it received’t execute your loop’s block instantly as a result of it’s ready for extra information.

To do this out, I wrote a easy server-side script to ship strains of knowledge separated by one second every, and we will write a small SwiftUI app to see that information arriving piece by piece:

struct ContentView: View {
    @State non-public var strains = [String]()
    @State non-public var standing = "Fetching…"

    var physique: some View 
        VStack 
            Textual content("Depend: (strains.depend)")
            Textual content("Standing: (standing)")
        
        .job 
                do 
                    let url = URL(string: "https://hws.one/slow-fetch")!

                    for strive await line in url.strains 
                        strains.append(line)
                    

                    standing = "Carried out!"
                 catch 
                    standing = "Error thrown."
                
            
    
}

When that runs you’ll see the road depend transfer upwards till the loop lastly finishes – and it’s all utilizing await so it received’t block the UI whereas the community fetch occurs.

And there’s extra…

We’re utilizing URL.strains right here, however actually that’s only a useful comfort wrapper across the information arriving asynchronously. If you’d like the info instantly, you possibly can bypass the 16KB buffer completely and browse the URL’s resourceBytes property – you’ll get each single byte delivered to you individually to do with as you please, a bit like a community hosepipe spraying the bytes freely till they cease coming:

for strive await byte in url.resourceBytes 
    let string = UnicodeScalar(byte)
    print(string)

In fact, it’s also possible to go the different approach and ask Swift to fetch all the info earlier than handing it to you. That is greatest achieved utilizing information(from:), like this:

let url = URL(string: "https://hws.one/slow-fetch")!
let (information, _) = strive await URLSession.shared.information(from: url)
let string = String(decoding: information, as: UTF8.self)
strains = string.elements(separatedBy: "n")

Tip: As that waits for all the info to return again earlier than persevering with, our instance app will seem to do nothing for 10 seconds.

Anyway, this was only a fast publish to precise my appreciation for URL.strains – it’s a small change within the grand scheme of issues, however I simply love its simplicity and sit up for it being utilized in actual apps.

Sponsor Hacking with Swift and reach the world’s largest Swift community!





Source link

Share30Tweet19
learningcode_x1mckf

learningcode_x1mckf

Recommended For You

The abstract Vapor service factory design pattern

by learningcode_x1mckf
February 1, 2023
0
The abstract Vapor service factory design pattern

I've written a number of articles about manufacturing unit design patterns on my weblog and this time I might like to speak a couple of particular one, which...

Read more

SwiftNIO tutorial – The echo server

by learningcode_x1mckf
January 27, 2023
0
SwiftNIO tutorial – The echo server

Intoducing SwiftNIO In the event you used a excessive degree net framework, corresponding to Vapor, up to now, you would possibly had some interplay with occasion loops...

Read more

Introducing – Vapor cheatsheet – The.Swift.Dev.

by learningcode_x1mckf
January 23, 2023
0
Introducing – Vapor cheatsheet – The.Swift.Dev.

Out there on Gumroad Thanks for supporting my work by buying the cheatsheet. 🙏 Download now A whole Vapor framework reference for novices. greater than...

Read more

iCloud Programming Tutorial for iOS: An Introduction

by learningcode_x1mckf
January 18, 2023
0
iCloud Programming Tutorial for iOS: An Introduction

Editor’s observe: This week, we work with Ziad Tamim once more to provide you an introduction of iCloud programming. You’ll learn to save and retrieve knowledge from iCloud.On...

Read more

Easy multipart file upload for Swift

by learningcode_x1mckf
January 18, 2023
0
Easy multipart file upload for Swift

I imagine that you've got already heard in regards to the well-known multipart-data add method that everybody likes to add recordsdata and submit type knowledge, but when not,...

Read more
Next Post
InAppBrowser tool reveals hidden JavaScript injections

InAppBrowser tool reveals hidden JavaScript injections

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Related News

Javascript Is Everywhere. Even MSDOS

Javascript Is Everywhere. Even MSDOS

October 2, 2022
VIPER best practices for iOS developers

VIPER best practices for iOS developers

September 30, 2022
We Must Kill ‘Dinosaur’ JavaScript | Microsoft Open Sources 3D Emoji

We Must Kill ‘Dinosaur’ JavaScript | Microsoft Open Sources 3D Emoji

December 1, 2022

Browse by Category

  • C#
  • C++
  • Java
  • JavaScript
  • Python
  • Swift

RECENT POSTS

  • JobRunr, the Java Scheduler Library, Released Version 6.0 – InfoQ.com
  • An Introduction to Lodash and Its Benefits for JavaScript Developers – MUO – MakeUseOf
  • "Used properly, Python is not slower than C++" – eFinancialCareers (US)

CATEGORIES

  • C#
  • C++
  • Java
  • JavaScript
  • Python
  • Swift

© 2022 Copyright Learning Code

No Result
View All Result
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#

© 2022 Copyright Learning Code

Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?