PATHis a system-level variable that holds a list of directories. When you enter a command in the terminal, it’s shorthand for a program with the same name. The system looks in each of thePATHdirectories for the program corresponding to the command. When it finds a matching program, it runs it. If it doesn’t find a match, it raises an error.
for instance if you type $echo $PATH, first terminal will look for the echo program in system, if its present terminal will execute it with argument $PATH. then its echo programs responsibility to print something on the screen.
if you want to know where the echo program is located in your system you can find it using which program. like this $ which echo. again which is also a program located somewhere in system even you can do this $ which which
If we enter a command name that doesn’t exist in any PATH directory, we get an error: command not found.
Thursday, 8 April 2021
Storing Codable structs on the disk
Most of our apps are REST clients for some backends. During the development of this kind of apps, we want to keep it working offline. In this case, we have to cache data somewhere locally on the device to make it readable without an internet connection.
Apple provides a CoreData framework, which is the best way to store your app data locally. It has a lot of excellent features which help you to boost development. However, it is tough to use it as a simple cache. Most of the time, we just need to display cached data without any additional manipulations. In my opinion, all we need is pure disk storage. This week we will discuss how easily we can implement straightforward disk store for our Codable structs.
Let’s start with defining a couple of protocols for our storage logic. I want to separate access to writable and readable parts of the storage, and this is where we can use protocol composition feature of Swift language.
Here we have two protocols describing reading and writing operations on storage. Protocols also provide an asynchronous version for reading and writing actions with completion handlers. We also create typealias Storage, which is a composition of two protocols. Now we can start to work on DiskStorage class which implements our Storage protocols.
First of all, let’s describe some variables for root path of our storage, DispatchQueue for asynchronous work and FileManager, which we will use to navigate through the file system.
Next step is the implementation of the writable part of our storage. It is a little bit tricky because the key is a path to our data on the file system. That’s why we need append the key to our root path and generate new URL for the storing data. New URL can contain subfolders, that’s why we create a createFolders function which creates needed folders according to the path.
Here is the readable part of our Storage protocol, where we implement data fetching for a passed key. Again, we use the key as a path to our data on disk. Now we have a working example of straightforward disk storage. Next step is implementing a simple adapter for our DiskStorage class, which will handle JSON coding/decoding.
CodableStorage class wraps our DiskStorage class to add JSON coding-decoding logic. It uses generic constraints to understand how to decode and encode data. It’s time to use our CodableStorage in real life sample.
In the code sample above, you can see the usage of CodableStorage class. We create DiskCache class instance which uses a temporary folder to store data. Timeline is a simple codable struct representing an array of strings which we store in our CodableStorage.