Read local file with Combine

Angelo E. Saber
3 min readMar 30, 2022

--

Read local file with Combine

The Combine framework provides a declarative Swift API for processing values over time. These values can represent many kinds of asynchronous events. Combine declares publishers to expose values that can change over time, and subscribers to receive those values from the publishers.

If you have a local file inside our app and you have to read it .

Let start with code to read the file content :

At the part 1 , we are reading the file content and convert it from string to data .

1- At line 5 we are creating an extension of Bundle (Bundles organize their contained resources into well-defined subdirectories, and bundle structures vary depending on the platform and the type of the bundle) .

2- At line 7 we are declaring a method that have a name readFile method . this method is returning a AnyPublisher<Data, Error> (Any publisher is type-erased struct that conforms the Publisher protocol. Type erasure allows you to hide details about the publisher that you may not want to expose to subscribers ) .

3- At line 8 by using self.url(forResource: file, withExtension: nil) method will return the content of local file as a String . ( Note : the file parameter is the file path inside app)

4- At line 9 we are adding combine publisher to self.url method to use an operate like try map and mapError.

5- At line 10 , trymap operator is using the return from self.url(forResource: file, withExtension: nil) at line 4 to the convert the content of file from string to Data .

6- from line 11 to line 14 , we are starting to convert file content from String of Data by using Data(contentsOf: string) method . ( Note : string is the file path inside app)

7- At line 16 mapError operator is returning error if any error that will be happen during the convert from string to data .

8- at line 18 method will return a eraseToAnyPublisher()( eraseToAnyPublisher() is a type erasure that returning an instance of AnyPublisher.eraseToAnyPublisher() is using as the last element in a chained pipeline, to simplify the type returned .)

At the part 2 , Now we need to build our Decodable Struct:

we are creating a Struct of Decodable .Decodable Struct to decode to the content file from JSON Data to Struct .

At the part 3 , we are decoding the output from readfile method to generic parameter :

1- At line 1 we are creating a method that have name a decodeable . decodeable is a method generic . Method Generic is written in angle brackets (< placeholder : Type>) after the method name but before its parameters . The decodeable function above can take input of Decodable. Decodable is a protocol . When you want to convert JSON data (file content) into a struct, you can conform your object to Decodable .

2- At line 3 we are calling the readFile method that is returning a AnyPublisher<Data, Error> .

3- we are using decode(type:decoder:) with a JSONDecoder() to decode data received from a readfile method using the Decodable protocol.

4- At line 5 mapError operator is returning error if any error that will be happen during the convert from JSON Data to Decodable type .

5- at line 9 method will return a eraseToAnyPublisher() .

Part 4 : Implementation of read local file :

1- at line 1 we are calling Bundle class to subscribe the method decodeable.

2- a decodeable method is returning a publisher that is publishing to a sink(receiveCompletion:receiveValue:) . sink operater’s receiveValue closure that prints all players to the console. sink operator’s receiveCompletion closure indicates the successful termination of the publisher or display an error if error will happen .

Now Full Code for reading local file with Combine

Thanks for reading my Article .

--

--

Angelo E. Saber

Hi, I’m @AngeloXcode , 👀 I’m interested in Swift,SwiftUI and Objective c — 🌱 I’m currently learning SwiftUI