Ich lade Bild vom Server und wenn Bild heruntergeladen wird, muss ich dieses Bild in Coredata mit ID speichern. Hier ist der CodeWie wird URLSession.shared.dataTask in einer Schleife aufgerufen?
for actionDict:JSON in actionData
{
if(actionDict["ProtocolImage"].stringValue.isValidURL())
{
URLSession.shared.sessionDescription = "\(actionDict["Id"].intValue)"
URLSession.shared.dataTask(with: URL(string: actionDict["ProtocolImage"].stringValue)!, completionHandler: { (data, response, error) in
if data != nil{
let image = UIImage(data: data!)
if image != nil{
ProtocolActionModel.updateValueWhere(id: Int64(URLSession.shared.sessionDescription!)!, key: "protocolImage", value:(UIImagePNGRepresentation(image!)! as NSData))
}
}
}).resume()
}
}
(actionDict["Id"].intValue
ist id mit Bild zugeordnet, die ich in Coredata mit Bild speichern möchten, aber wenn ich es speichern ist immer auf letzte Element-ID von für Schleife.
actionData
ist
{
data = (
{
CheckDesc = vcnhfgb;
CheckName = hjnhfnmjh;
CheckedImage = (
);
CreatedDate = "/Date(1487201399293)/";
Id = 3;
IsChecked = 0;
ProtocolId = 4;
ProtocolImage = "http://example.com/ApplicationImages/522cb086-0b0d-4ad9-b5ae-79f6f9a9fbf2.jpg";
Sorting = "<null>";
},
{
CheckDesc = bfghrthbn;
CheckName = ddfgb;
CheckedImage = (
);
CreatedDate = "/Date(1499167861457)/";
Id = 71;
IsChecked = 0;
ProtocolId = 4;
ProtocolImage = "http://example.com/ApplicationImages/860e68e4-187c-4e49-a936-088c97c500b2.png";
Sorting = 41;
},
{
CheckDesc = bnvbncyh;
CheckName = nbnyhjhg;
CheckedImage = (
);
CreatedDate = "/Date(1499167894280)/";
Id = 72;
IsChecked = 0;
ProtocolId = 4;
ProtocolImage = "http://example.com/ApplicationImages/e4311494-27ad-4d12-958b-68e0c705a94d.png";
Sorting = 42;
},
{
CheckDesc = hjbnvbytuu;
CheckName = nmnmhjgh;
CheckedImage = (
);
CreatedDate = "/Date(1499167918147)/";
Id = 73;
IsChecked = 0;
ProtocolId = 4;
ProtocolImage = "http://example.com/ApplicationImages/15ac0300-8877-4503-8c6d-957b5248e829.png";
Sorting = 43;
},
{
CheckDesc = "";
CheckName = tyres;
CheckedImage = (
);
CreatedDate = "/Date(1499172466890)/";
Id = 74;
IsChecked = 0;
ProtocolId = 4;
ProtocolImage = "<null>";
Sorting = 44;
}
);
message = success;
status = 1;
}
reponse
<NSHTTPURLResponse: 0x600000634720> { URL: http://example/ApplicationImages/860e68e4-187c-4e49-a936-088c97c500b2.png } { status code: 200, headers {
"Accept-Ranges" = bytes;
"Content-Length" = 254882;
"Content-Type" = "image/png";
Date = "Wed, 05 Jul 2017 05:31:46 GMT";
Etag = "\"e09aa43b9f4d21:0\"";
"Last-Modified" = "Tue, 04 Jul 2017 11:31:01 GMT";
Server = "Microsoft-IIS/7.5";
"X-Powered-By" = "ASP.NET";
"X-Powered-By-Plesk" = PleskWin;
} }
Bitte geben Sie eine Lösung, die mein Problem zu lösen.
'(actionDict [„id“]. IntValue' ist für alle Bilder gleich? –
@DharmeshKheni nicht alle sind verschieden –
Grund Ihrer for-Schleife, bevor Daten speichern in der Datenbank ausgeführt wird am Ende sein könnte. Und daher Der Verweis wird auf den letzten Wert der Schleife gesetzt. Debuggen Sie es und überprüfen Sie. – Gihan