-
Notifications
You must be signed in to change notification settings - Fork 1
/
Users.swift
119 lines (106 loc) · 5.32 KB
/
Users.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
//
// Users.swift
// ios swift boiler
//
// Created by Godson Ukpere on 3/14/16.
// Copyright © 2016 Godson Ukpere. All rights reserved.
//
import CoreData
import SwiftyJSON
@objc(Users)
class Users: NSManagedObject {
@NSManaged var name: String
@NSManaged var platform: String
@NSManaged var email: String
@NSManaged var user_id: String
@NSManaged var img: String
@NSManaged var oauth_token: String
@NSManaged var oauth_token_secret: String
@NSManaged var bg_img: String
@NSManaged var username: String
@NSManaged var desc: String
static func fetch (model:String, sortBy:String? = nil, sortAsc:Bool = false, format:String? = nil, args:String? = nil, callback:(context:NSManagedObjectContext, result:[NSManagedObject]?, error: NSError?)->()) {
let appDelegate: AppDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate)
let context: NSManagedObjectContext = appDelegate.managedObjectContext
let fetchRequest = NSFetchRequest(entityName: model)
if format != nil && args != nil {
fetchRequest.predicate = NSPredicate(format: format!, args!)
}
// If sort query parameter {sortBy} is set
if sortBy != nil {
fetchRequest.sortDescriptors = [NSSortDescriptor(key: sortBy, ascending: sortAsc)]
}
do {
let result:[NSManagedObject] = try context.executeFetchRequest(fetchRequest) as! [NSManagedObject]
if result.count != 0 {
callback(context: context, result: result, error: nil)
} else {
callback(context: context, result: nil, error: nil)
}
} catch let error as NSError {
callback(context: context, result: nil, error: error)
}
}
static func managedObject (callback:(context:NSManagedObjectContext, user:Users)->()) {
let appDelegate: AppDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate)
let context: NSManagedObjectContext = appDelegate.managedObjectContext
let en = NSEntityDescription.entityForName(Const.MODEL_USERS, inManagedObjectContext: context)
callback(context: context, user: Users(entity: en!, insertIntoManagedObjectContext: context))
}
static func saveUser (view:UIViewController? = nil, authUser:JSON, token:String? = nil, callback: (error: NSError?)->()) {
Helpers.currentUser = authUser;
fetch(Const.MODEL_USERS) { context, result, error in
if error == nil && result != nil {
let user = result!.first
if let oauthToken:String = authUser[Const.KEY_OAUTH_TOKEN].string {
user?.setValue(oauthToken, forKey: Const.KEY_OAUTH_TOKEN)
}
if let oauthTokenSecret = authUser[Const.KEY_OAUTH_TOKEN_SECRET].string {
user?.setValue(oauthTokenSecret, forKey: Const.KEY_OAUTH_TOKEN_SECRET)
}
user?.setValue(authUser[Const.KEY_USERNAME].stringValue, forKey: Const.KEY_USERNAME)
user?.setValue(authUser[Const.KEY_NAME].stringValue, forKey: Const.KEY_NAME)
user?.setValue(authUser[Const.KEY_EMAIL].stringValue, forKey: Const.KEY_EMAIL)
user?.setValue(authUser[Const.KEY_DESCRIPTION].stringValue, forKey: Const.KEY_DESCRIPTION)
Helpers.saveManagedContext(view!, context: context, callback: callback)
} else {
managedObject({ context, user in
user.name = authUser[Const.KEY_NAME].stringValue
user.desc = authUser[Const.KEY_DESCRIPTION].stringValue
user.oauth_token = authUser[Const.KEY_OAUTH_TOKEN].stringValue
user.oauth_token_secret = authUser[Const.KEY_OAUTH_TOKEN_SECRET].stringValue
user.email = authUser[Const.KEY_EMAIL].stringValue
user.user_id = authUser[Const.KEY_USER_ID].stringValue
user.username = authUser[Const.KEY_USERNAME].stringValue
user.img = authUser[Const.KEY_IMG].stringValue
user.bg_img = authUser[Const.KEY_BG_IMG].stringValue
user.platform = authUser[Const.KEY_PLATFORM].stringValue
Helpers.saveManagedContext(view, context: context, callback: callback)
})
}
}
}
static func deleteData (limit:Int = 0, callback: ((count:Int, error:NSError?) -> ())? = nil) {
fetch(Const.MODEL_USERS, sortBy: "user_id", sortAsc: true) { (context, result, error) -> () in
var count:Int = 0
if result != nil {
for managedObject in result! {
if count == limit && limit != 0 {
break
}
context.deleteObject(managedObject)
count += 1
}
Helpers.saveManagedContext(context: context) {
errror in
if callback != nil {
callback!(count: count,error: error)
}
}
}
if callback != nil {
callback!(count: count, error: nil)
}
}
}
}