قالب وردپرس درنا توس
Home / IOS Development / Pantry, a light struct caching library

Pantry, a light struct caching library



Review one of my latest Swift apps, I realized how often I keep (or will continue) small pieces of data.

  • Feature flag (has the user access to x?)
  • User preferences (Turn on / off reminder notifications)
  • Track flow (has the user been on this screen before?)
  • Sharing data (prefill field with previously captured data)

And when I think about what to use for Persistence, I'm thinking about this post on NSHipster:

  NSHipster

And that's true for Objective-C. NSKeyedArchiver was the way to go for many projects. But we have come to expect another definition of " Not a pain in the ass " since transition from Objective-C to Swift, and in this case, I think we deserve something better than NSKeyedArchiver ]. Not just something written in Swift (where there is any), but something that feels at home with the rest of the Swift code.

This is Pantry, a simple and inventive way to save basic types and structures in Swift without setup

  Pantry

This started as a project to save only native structures because it's something like NSKeyedArchiver simply can not do . I use structures everywhere, and I was frustrated by turning them into @objc WhateverClass: NSObject if I wanted to sustain them in meaningful lengths.

It grew out of the first use of Being a little more general because that's how I used it. As soon as I realized that I could easily save structures, I've started thinking about what I could achieve with persistent basic types in a very straightforward way.

It's best shown instead of explained, let's get some usage: [1

9659014] Easy expired cache functionality

At its most basic, Pantry is a nice cache for basic types. In this example, a feature is turned on or off with some expensive operation (network request, a lot of processing, etc.), but the status can be changed frequently so we will not retrieve it once and cache it forever.

Instead, we look for a Pantry value and report the results if it exists. If it does not exist, we do our expensive operation, and then you post the result as a buffer Bool for 10 minutes. la available: Bool = Pantry.unpack ( "promptAvailable" ) {
Completion (Available: [19659018] Available } Otherwise {[19659026] anExpensiveOperationToDetermineAvailability ({ (Available) -> () in
Storage.pack , key: "promptAvailable" [19659019] expires: . Seconds ( 60 * 10 ))
Completion : available)
})
} [19659050] At the end of 10 minutes, Pantry.unpack () ` will return zero again and you can do your expensive operation to determine status.

The advantage of using Pantry over any of the existing options like AwesomeCache or Haneke is that you can also sift structures with minimal boilerplate code so your cache that might be unstructured authenticity with magic keys or multiple cache values ​​is now only a heavily written structure with transparent content.

Automagic Persistent Variables

Perhaps the most interesting usage case I have created when working with Pantry is the term property on a class or structure that automatically persists over the launches. This feels strange and unintentional in the beginning, but I have found a few places where it is incredibly helpful.

Fortunately, between Pantry and Swift, this is quite simple to set up.

  was    autopersist:    string?          
             la    nyVerdi    =    nyVerdi    {
              Pantry.pack (nyVerdi,    key:    "autopersist" ) 
        } 
    } 
      get    {
          return    Pantry.unpack (  "autopersist" ) [19659026] } 
} 

This is a standard feature of the viewer or whatever you have. It is written to disk when you write to the variable and read from disk as you read it. There is nothing special in itself, but simplicity is what makes this a nice part of the overall control unit's composition.

And just as before, this is a simple example of a String where a structure with a few useful areas can be replaced.

Alternatives

In both of these situations, you need to write much more code that deals with NSKeyedArchiver only to have this feature work with standard NSCoding compatible types. Define where your cache lives, manage reading and writing, and even think about how your data is stored.

With Pantry, you get a line reader and writes for basic types and a minimal amount of setup code (only on decoding, not both ways!) Gives you support for arbitrary structures. There is significantly less effort than the option.

Dimensions for Pantry

A couple of items I have:
* Easy use / understanding
* Minimal boilerplate code
* Speed ​​

And equally important, things we do not need to do:
* Target C support
* Queries
* Cache Format Control

I want to be aware of the things I do not consider important for two reasons:

a) A tool does not support each mode of use. In fact, I would say that the best tools for the job are those built with a clear vision of a job in mind. And thanks to open source software, you can customize this to another job if it works better for you.

b) The user does not have to decide every detail. Yes, we can let you decide if you want your data in binary or plot format, or encrypted on disk. But there are more decisions for you and more chance that some of you will get it wrong. Instead, we will do the work to ensure that it complies with our goals: simple, easy and fast.

Previously, Storage

made the remarkable parts of how Pantry works at Swift Summit SF in October only at the time it was called Storage . A persistent question was about how well this would hold up large objects with many structures and substructures, or how to ask for these objects. Afterwards, I realized that the name Storage was misleading: it's not a general storage for your app, but it's great for these smaller operating states that I've outlined here. It is still early enough that the change to the Pantry was painless and fast.

For cases of large networking of objects, you're always better off with a real data storage like Core Data or Realm. Pantry will never do that because it's not one of our goals. Many people have devoted many hours to making these tools good, you should use them as they are meant.

But these things work well side by side. From my perspective, there is a need for minimal, easy-to-use way to store and retrieve small data in Swift, regardless of your primary storage mechanism. Pantry is currently beta software, but I have written many tests and I use it in production today. Get help with github or just start using it in your projects with Cocoapods or Carthage.

Pantry is the second open source framework program that has created from our work on treats – the first was PermissionScope that recently passed 1700 stars at Github. Please contact if this kind of thing interests you.


Questions or comments? Find us at twitter or send a problem on github




Source link