· 9 years ago · Jan 13, 2017, 10:52 AM
1if error != nil
2 {
3 print("error is (error)")
4 return;
5 }
6
7 //parsing the response
8 do
9 {
10
11
12
13
14
15 //converting resonse to NSDictionary
16 let myJSON = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as? NSDictionary
17 //print("DOWNLOADED DATA")
18 //print(myJSON!)
19
20 //parsing the json
21 if let parseJSON = myJSON
22 {
23 let Status = parseJSON["status"] as! Int
24 let Code = parseJSON["code"] as! Int
25 //print("status:(Status)")
26 //print("code:(Code)")
27 if (Status == 1)
28 {
29
30
31 let Result = parseJSON["result"]!
32
33 //print("Result=(Result)")
34 //print("CHECKOUT")
35
36 for res in Result as! NSArray
37 {
38 let date = res["date"] as! String
39 let data = res["data"] as! String
40
41 print("data")
42 print(data)
43
44 if (data != "")
45 {
46 //ModelManager.sharedInstance.insertingRecordDataToDatabaseAfterDownload(self.id, email: self.Email, createdOn: date, record: data)
47
48 //function for Saving the DataRecords into the Database
49
50 let URL = try! NSFileManager.defaultManager().URLForDirectory(.DocumentDirectory, inDomain: .UserDomainMask, appropriateForURL: nil , create: false).URLByAppendingPathComponent("PersonalRecordAppDataBase.sqlite")
51
52
53
54 guard let recordDB = FMDatabase (path : URL.path) else
55 {
56 print("unable to create the database")
57 return
58 }
59 print(URL)
60
61
62 guard recordDB.open()
63 else
64
65 {
66 print("Database is not open or unable to connect")
67 return
68 }
69 do
70 {
71
72 try! recordDB.executeUpdate ("create table IF NOT EXISTS recordDataTable (ID integer, Email text, createdOn TEXT, updatedOn TEXT, Record text, Lastseen TEXT)" , values: nil);
73 try! recordDB.executeUpdate ("insert into recordDataTable (ID, Email, createdOn, updatedOn, Record, Lastseen) values(?,?,?,?,?,?)", values : [self.id, self.Email, date, self.Current! , data, ""])
74
75
76
77 }
78
79 catch let error as NSError
80 {
81 print("failed: (error.localizedDescription)")
82 }
83
84 recordDB.close()
85
86
87 }
88
89 let image = res["images"] as! NSArray
90 print(image.count)
91 //print(image)
92 //while (image.next != nil)
93 self.lastComponentArray.removeAll()
94 for item in image
95 {
96
97
98 if item as! String != ""
99 {
100 //print("image : (image)")
101
102 //print("CHECKEDIN")
103 //print(image)
104 print(item)
105
106
107 let url = NSURL(string: item as! String)
108
109
110 let request = NSURLRequest(URL: url!)
111 NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue())
112 {
113 (response: NSURLResponse?, data: NSData?, error: NSError?) -> Void in
114
115
116
117 let imagedata = UIImage(data: data!)
118
119 let lastComponent = url?.lastPathComponent
120 //print("itemLASTCOMPONENT = (lastComponent!)")
121 self.lastComponentArray.append(lastComponent!)
122 //print(self.lastComponentArray)
123
124 //ModelManager.sharedInstance.insertingRecordImagesToDatabaseAfterDownload(self.id, email: self.Email, createdOn: date, lastComponent: lastComponent!)
125
126 // Saving images to the Document Directory
127
128 let fileManager = NSFileManager.defaultManager()
129 let paths = (NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as NSString).stringByAppendingPathComponent("/(lastComponent!)")
130 print("Imagepaths=(paths)")
131 let imageData = UIImageJPEGRepresentation(imagedata!, 0.2)
132 fileManager.createFileAtPath(paths as String, contents: imageData, attributes: nil)
133
134
135
136 //function for Saving the RecordImages into the Database
137
138 let URL = try! NSFileManager.defaultManager().URLForDirectory(.DocumentDirectory, inDomain: .UserDomainMask, appropriateForURL: nil , create: false).URLByAppendingPathComponent("PersonalRecordAppDataBase.sqlite")
139
140
141
142 guard let recordDB = FMDatabase (path : URL.path) else
143 {
144 print("unable to create the database")
145 return
146 }
147 print(URL)
148
149
150 guard recordDB.open()
151 else
152
153 {
154 print("Database is not open or unable to connect")
155 return
156 }
157 do
158 {
159 try recordDB.executeUpdate("create table IF NOT EXISTS recordImagesTable (ID integer, Email text, createdOn TEXT, updatedOn TEXT, recordImages NSDate)" , values: nil);
160 try! recordDB.executeUpdate ("insert into recordImagesTable (ID,Email,createdOn, updatedOn, recordImages) values(?,?,?,?,?)", values : [self.id, self.Email, date, self.Current!, lastComponent!, ""])
161
162
163 }
164 catch let error as NSError
165 {
166 print("failed: (error.localizedDescription)")
167 }
168
169 recordDB.close()
170
171 }
172 self.tableView.reloadData()
173
174
175 // print("lastComponentArray")
176 //print(self.lastComponentArray)
177
178 }
179
180
181
182
183 }
184 print("save to database")
185 // ModelManager.sharedInstance.insertingRecordImagesToDatabaseAfterDownload(self.id, email: self.Email, createdOn: date, lastComponent: self.lastComponentArray)
186
187
188 }
189
190 //showing the AlertView that Records has been Downloaded
191
192 let A = UIAlertController(title: "Done!!!", message: "Record downloaded", preferredStyle: .Alert)
193 let B = UIAlertAction(title: "ok", style:UIAlertActionStyle.Default, handler: nil)
194 A.addAction(B)
195 self.presentViewController(A, animated: true, completion: nil)
196
197 }
198
199 if (Status == 0)
200 {
201 print("No data for Selected month")
202 }
203
204
205 }
206
207 }
208
209
210 catch
211 {
212 print(error)
213 }
214
215
216
217 }
218 //executing the task
219 task.resume()
220 }