Skip to content

Commit 8a179cb

Browse files
author
Clément Le Provost
committed
Merge branch 'sdk'
2 parents ca39e4c + 2aaacfc commit 8a179cb

File tree

9 files changed

+662
-189
lines changed

9 files changed

+662
-189
lines changed

AlgoliaSearch-Client-Swift.podspec

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Pod::Spec.new do |s|
1111
s.ios.deployment_target = '7.0'
1212
s.osx.deployment_target = '10.10'
1313

14-
# By default, do not require the offline SDK.
14+
# By default, do not require the offline Core.
1515
s.default_subspec = 'Online'
1616

1717
# Online-only API client.
@@ -27,14 +27,14 @@ Pod::Spec.new do |s|
2727

2828
# Offline-enabled API client.
2929
# NOTE: Disabled until release.
30-
# s.subspec 'Offline' do |offline|
31-
# offline.dependency 'AlgoliaSearchSDK-iOS'
32-
# # Activate SDK-dependent code.
33-
# # WARNING: Specifying the preprocessor macro is not enough; it must be added to Swift flags as well.
34-
# offline.pod_target_xcconfig = {
35-
# 'GCC_PREPROCESSOR_DEFINITIONS' => 'ALGOLIA_SDK=1',
36-
# 'OTHER_SWIFT_FLAGS' => '-DALGOLIA_SDK'
37-
# }
38-
# offline.source_files = 'Source/Offline/*.swift'
39-
# end
30+
s.subspec 'Offline' do |offline|
31+
offline.dependency 'AlgoliaSearchOfflineCore-iOS'
32+
# Activate Core-dependent code.
33+
# WARNING: Specifying the preprocessor macro is not enough; it must be added to Swift flags as well.
34+
offline.pod_target_xcconfig = {
35+
'GCC_PREPROCESSOR_DEFINITIONS' => 'ALGOLIA_SDK=1',
36+
'OTHER_SWIFT_FLAGS' => '-DALGOLIA_SDK'
37+
}
38+
offline.source_files = 'Source/Offline/*.swift'
39+
end
4040
end

Source/Client.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ import Foundation
114114
// WARNING: Those headers cannot be changed for the lifetime of the session.
115115
let version = NSBundle(forClass: self.dynamicType).infoDictionary!["CFBundleShortVersionString"] as! String
116116
let fixedHTTPHeaders = [
117-
"User-Agent": "Algolia for Swift \(version)",
118117
"X-Algolia-Application-Id": self.appID
119118
]
120119
let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
@@ -125,6 +124,8 @@ import Foundation
125124

126125
// Other headers are likely to change during the lifetime of the session: they will be passed for every request.
127126
headers["X-Algolia-API-Key"] = self.apiKey // necessary because `didSet` not called during initialization
127+
// NOTE: This one will not change... except it can be overridden by the offline client:
128+
headers["User-Agent"] = "Algolia for Swift \(version)"
128129
}
129130

130131
/// Set an HTTP header that will be sent with every request.

Source/Error.swift

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,18 @@ public enum StatusCode: Int {
6161
}
6262
}
6363

64-
/// Determine if a given error is transient or not.
65-
/// "Transient" means retrying an identical request at a later time might lead to a different result.
66-
///
67-
func isErrorTransient(error: NSError) -> Bool {
68-
if (error.domain == Client.ErrorDomain) {
69-
return StatusCode.isServerError(error.code)
70-
} else if (error.domain == NSURLErrorDomain) {
71-
return true
72-
} else {
73-
return false
64+
extension NSError {
65+
/// Determine if this error is transient or not.
66+
/// "Transient" means retrying an identical request at a later time might lead to a different result.
67+
///
68+
func isTransient() -> Bool {
69+
if (domain == Client.ErrorDomain) {
70+
return StatusCode.isServerError(code)
71+
} else if (domain == NSURLErrorDomain) {
72+
return true
73+
} else {
74+
return false
75+
}
7476
}
7577
}
78+

Source/Helpers.swift

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,21 @@ extension String {
3131
}
3232
}
3333

34-
// Collection shuffling taken from <http://stackoverflow.com/questions/24026510/how-do-i-shuffle-an-array-in-swift>.
34+
// MARK: - Memory debugging
35+
36+
// NOTE: Those helpers are not used in the code, but let's keep them because they can be handy when debugging.
37+
38+
/// Log the initialization of an object.
39+
func logInit(object: AnyObject) {
40+
print("<INIT> \(unsafeAddressOf(object)) (\(object.dynamicType)) \(object.description)")
41+
}
42+
43+
/// Log the termination ("de-initialization" in Swift terms) of an object.
44+
func logTerm(object: AnyObject) {
45+
print("<TERM> \(unsafeAddressOf(object)) (\(object.dynamicType)) \(object.description)")
46+
47+
// MARK: - Collection shuffling
48+
// Taken from <http://stackoverflow.com/questions/24026510/how-do-i-shuffle-an-array-in-swift>.
3549

3650
extension CollectionType {
3751
/// Return a copy of `self` with its elements shuffled.

Source/Index.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,17 @@ import Foundation
3939
self.indexName = indexName
4040
urlEncodedIndexName = indexName.urlEncode()
4141
}
42+
43+
// MARK: - Utils
44+
45+
override public var description: String {
46+
get {
47+
return "Index{\"\(indexName)\"}"
48+
}
49+
}
4250

51+
// MARK: - Core API operations
52+
4353
/// Add an object to this index.
4454
///
4555
/// - parameter object: The object to add.

0 commit comments

Comments
 (0)