Skip to content

Commit

Permalink
Cache access now thread safe
Browse files Browse the repository at this point in the history
  • Loading branch information
evermeer committed Mar 25, 2017
1 parent bb07909 commit eccbc03
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 7 deletions.
20 changes: 19 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,25 @@ For more information see the [installation instructions](https://github.com/ever

## Master

Is published as version 4.4.1
Is published as version 4.6.0

## 4.6.0 (2017-3-25)

#### Bug Fixes

* Cache access now thread safe

## 4.5.1 (2017-3-18)

#### Enhancements

* default implementation for customConverter

## 4.5.0 (2017-3-2)

#### Enhancements

* added the customConverter function

## 4.4.1 (2017-2-22)

Expand Down
2 changes: 1 addition & 1 deletion EVReflection.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "EVReflection"
s.version = "4.5.1"
s.version = "4.6.0"
s.summary = "Reflection based (dictionary, JSON or XML) object mapping (including extensions for Alamofire and Moya with RxSwift or ReactiveSwift)"

s.description = <<-EOS
Expand Down
Binary file not shown.
19 changes: 14 additions & 5 deletions Source/EVReflection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,9 @@ final public class EVReflection {
}


static var properiesCache = NSMutableDictionary()
static var typesCache = NSMutableDictionary()
private static var properiesCache = NSMutableDictionary()
private static var typesCache = NSMutableDictionary()
private static var queue = DispatchQueue(label: "nl.evict.evreflection.cache")

/**
Convert an object to a dictionary while cleaning up the keys
Expand Down Expand Up @@ -160,7 +161,13 @@ final public class EVReflection {

let key: String = "\(swiftStringFromClass(theObject)).\(conversionOptions.rawValue)"
if isCachable {
if let p = properiesCache[key] as? NSDictionary, let t = typesCache[key] as? NSDictionary {
var p: NSDictionary?
var t: NSDictionary?
queue.sync {
p = properiesCache[key] as? NSDictionary
t = typesCache[key] as? NSDictionary
}
if let p = p, let t = t {
return (p, t)
}
}
Expand All @@ -173,8 +180,10 @@ final public class EVReflection {
tdict = types

if isCachable && typesCache[key] == nil {
properiesCache[key] = pdict!
typesCache[key] = tdict!
queue.sync {
properiesCache[key] = pdict!
typesCache[key] = tdict!
}
}
return (pdict!, tdict!)
}
Expand Down

0 comments on commit eccbc03

Please sign in to comment.