I am trying to save the following dictionary to my Firebase Database from an iOS/Objective-C application:
NSMutableDictionary* enrollmentDicc = {
"cleaning_tasks" = 3;
"eat_drink_dairy" = 2;
"eat_gluten" = 1;
"eat_meat" = 1;
"flor_lay_on_mat" = 2;
"garden" = 1;
"hours_spend_standing" = 1;
"level_of_flexibilitity" = 2;
"lift_or_carry" = "3,4,5";
"sit_bed_propped" = 3;
"strech_excercise" = "4,5,6";
"tight_calves" = 2;
"type_shoes" = "4,5,6";
"cause_back_pain" = "3,4";
"chill_sitting_room" = 1;
"cross_legs_sit_down" = 2;
diagnosis = "3,4";
"drive_everyday_hours" = 1;
email = "ignacio.orona+tw#gmail.com";
"has_diagnosis" = 1;
"hours_sitting_down" = 2;
"how_did_you_hear" = 3;
name = ignacio;
painLevelthisMoment = 10;
"pain_location" = 2;
processed = 0;
"sit_soft_couch" = 2;
"sleep_position" = 1;
treatments = "4,10";
For that purpose I use the following code:
-(void) apiPostEnrollment:(NSDictionary*)enrollmentDicc withCompletion:(SingleResponseCompletionBlock)completionBlock
{
_dbNodeRef = [[[FIRDatabase database] referenceWithPath:#"enrolledUsers"] childByAutoId];
[_dbNodeRef setValue:enrollmentDicc withCompletionBlock:^(NSError * _Nullable error, FIRDatabaseReference * _Nonnull ref){
completionBlock(ref,error);
}];
}
My problem is that the Dictionary does not get saved in Firebase, and I don't even have the response from the callback function (which ideally would give me some clue of why my data is not being stored. I've also turned on the Firebase debug mode with no luck either.
The problem actually was that there was a hidden character (\x) in some of the keys of the dictionary. Erased those wrong hidden characters that came from only god knows where, and it worked like charm.
Related
With iOS 9 i see in my iPhone console strange logs
These logs appears when the app connected to remote service,and maybe when the connection is slow, i use NSURLSession.
Someone has an idea about the meaning of this log, should I care of this:
-[NETAWDManager reportStats:metricID:] server 0x13cd19d70, container 0x13ce9dfe0, metrid 2686983, successfully reported:
<AWDLibnetcoreTCPConnectionReport: 0x13ce7af10> {
cellularFallbackReport = {
dataUsageSnapshotsAtNetworkEvents = (
{
bytesIn = 0;
bytesOut = 410;
}
);
"fallbackTimer_msecs" = 0;
fellback = 0;
networkEvents = (
"NETWORK_EVENT_DATA_STALL_AT_APP_LAYER"
);
"timeToNetworkEvents_msecs" = (
3325
);
};
clientIdentifier = "com.mydomain.myapp";
connectionStatisticsReport = {
DNSAnswersCached = 1;
"DNSResolvedTime_msecs" = 3;
RTTvariance = 169;
"appDataStallTimer_msecs" = 3;
appReportingDataStallCount = 1;
"bestRTT_msecs" = 359;
betterRouteEventCount = 0;
bytesDuplicate = 0;
bytesIn = 37000;
bytesOut = 410;
bytesOutOfOrder = 0;
bytesRetransmitted = 0;
cellularFallback = 0;
cellularRRCConnected = 0;
connected = 1;
connectedInterfaceType = "INTERFACE_TYPE_WIFI";
"connectionEstablishmentTime_msecs" = 308;
connectionReuseCount = 0;
"currentRTT_msecs" = 79;
"flowDuration_msecs" = 30750;
interfaceType = "INTERFACE_TYPE_WIFI";
kernelReportedStalls = 0;
kernelReportingConnectionStalled = 0;
kernelReportingReadStalled = 0;
kernelReportingWriteStalled = 0;
packetsDuplicate = 0;
packetsIn = 26;
packetsOut = 1;
packetsOutOfOrder = 0;
packetsRetransmitted = 0;
"smoothedRTT_msecs" = 275;
synRetransmissionCount = 0;
tcpFastOpen = 0;
"timeToConnectionEstablishment_msecs" = 315;
"timeToConnectionStart_msecs" = 7;
"timeToDNSResolved_msecs" = 7;
"timeToDNSStart_msecs" = 4;
trafficClass = 0;
};
delegated = 0;
reportReason = "REPORT_REASON_DATA_STALL_AT_APP_LAYER";
}
Based on the identifier cellularFallbackReport, this message may be related WiFi Assist. It's a new feature in iOS 9 that detects slow WiFi connections and automatically falls back to the phone's cellular radio.
This would be consistent with your observation that the log message occurs when you have a slow connection.
If you're able to reproduce the message reliably, try disabling WiFi Assist in settings and see if it disappears:
If that's really what it is, should you care about it? Probably not. The messages indicate the iOS system is dealing with network problems automatically on behalf of your app, and there's not much you can do about it.
let jsonResult1:NSDictionary = NSJSONSerialization.JSONObjectWithData(da!, options:NSJSONReadingOptions.MutableContainers , error: &error) as NSDictionary
println(jsonResult1)
getting below data in console
{
0 = {
"consulting_dr" = "DR.Appaji .";
"current_bed_nr" = 0;
"current_room_nr" = 0;
"discharge_date" = "03/03/2015 00:00";
"encounter_date" = "02/03/2015 12:45";
"encounter_nr" = 201503024000;
info = "";
"item_description" = "";
name = "Mrs. mythily S";
pdob = "01/08/1976";
pid = 100004;
psex = f;
pyear = "38 Years";
};
1 = {
dosage = 1;
drdate = "25/08/2014";
drnotes = "";
drugclass = Tablet;
duration = "5 day";
frequency = "";
medicine = "ACECLOFENAC+PARACETAMOL";
route = Oral;
tcomplients = "";
};
2 = {
BMI = "A:1:{s:4:\"SPO2\";s:1:\"1\";}";
BSA = "A:1:{s:4:\"SPO2\";s:1:\"1\";}";
"Dystolic_bp" = 29;
Height = 24;
Pulse = 26;
Respiration = 27;
"Systolic_bp" = 28;
Temp = 25;
Weight = 22;
dosage = 1;
drdate = "25/08/2014";
drnotes = "";
drugclass = Tablet;
duration = "5 day";
frequency = "";
medicine = RABEPRAZOLE;
route = Oral;
tcomplients = "";
};
}
how to store this in array
That is a Dictionary. It is easy to get just the data without the keys.
In Objective-C it would be :
NSArray *allData = [jsonResult1 allValues];
For swift it should be like: (not sure about syntax)
var allData = jsonResult1.allValues()
If you're up for it, you should give SwiftyJSON a try.
https://github.com/SwiftyJSON/SwiftyJSON
I've recently used it for an application that deals with a ton of JSON responses from a web service and SwiftyJSON has made it super easy for me to deal with JSON data in Swift. It will convert NSData to Dictionaries or Arrays seamlessly for you.
I'm requesting a SOAP Webservice and I parse the response to a NSDictionary. The XML response has multiple unique keys. Here's an example (already parse on the dictionary):
"typ:partits" = {
"typ:dadesPartit" = {
"typ:aforament" = 1;
"typ:codiEsdeveniment" = 2;
"typ:competicio" = GAMPER;
"typ:dataHoraConfirmada" = false;
"typ:dataPartit" = "08/18/14";
"typ:descripcioPartit" = "FCBARCELONA - CLUB LEON F.C.";
"typ:horaPartit" = "9:30:00 PM";
"typ:jornada" = 99;
"typ:partitActiuMenor" = true;
"typ:temporada" = "2014-2015";
"typ:tipusEsdeveniment" = 0;
},
"typ:dadesPartit" = {
"typ:aforament" = 1;
"typ:codiEsdeveniment" = 2;
"typ:competicio" = GAMPER;
"typ:dataHoraConfirmada" = false;
"typ:dataPartit" = "08/26/14";
"typ:descripcioPartit" = "FCBARCELONA - REAL MADRID";
"typ:horaPartit" = "9:30:00 PM";
"typ:jornada" = 101;
"typ:partitActiuMenor" = true;
"typ:temporada" = "2014-2015";
"typ:tipusEsdeveniment" = 0;
};
};
How can I iterate through these keys?, they are the same :( ...
I tried with "allObjects" but when I receive only one "typ:dadesPartit" object it treats it like an array instead of a NSDictionary.
it is returning one type as both your keys are same,
Try renaming the keys to separate names, and the values associated with those keys will come when u type allKeys
I'm trying to sort a JSON return into multiple sections according to each day. Here is the JSON return,
sales = (
{
"dis_dollars" = 0;
"dis_percent" = 0;
id = 111;
saleDay = 5;
saleMonth = 1;
saleTime = "20:02:39";
saleYear = 2014;
"total_price" = 25;
},
{
"dis_dollars" = 0;
"dis_percent" = 0;
id = 103;
saleDay = 2;
saleMonth = 1;
saleTime = "19:13:41";
saleYear = 2014;
"total_price" = 79;
},
{
"dis_dollars" = "0.55";
"dis_percent" = "0.10000000149012";
id = 101;
saleDay = 2;
saleMonth = 1;
saleTime = "10:41:11";
saleYear = 2014;
"total_price" = 22;
},
{
"dis_dollars" = 0;
"dis_percent" = 0;
id = 108;
saleDay = 1;
saleMonth = 1;
saleTime = "11:00:00";
saleYear = 2014;
"total_price" = 66;
}
);
}
I can get the number of sections if I do it this way
//...Connect to php script and get return json object
int saleDay = 0;
//Loop through json return
for (int i = 0;i < [sales count]; i++) {
NSDictionary* dict = [sales objectAtIndex:i];
Sale *eachSale =[[Sale alloc] initWithSaleId:[dict valueForKey:#"id"]
saleDay:[dict valueForKey:#"saleDay"]
saleMonth:[dict valueForKey:#"saleMonth"]
saleYear:[dict valueForKey:#"saleYear"]
saleTime:[dict valueForKey:#"saleTime"]
saleTotal:[dict valueForKey:#"total_price"]
saleDisDollars:[dict valueForKey:#"dis_dollars"]
saleDisPercent:[dict valueForKey:#"dis_percent"]];
NSLog(#"sale Day %#",eachSale.saleDay);
[_salesList addObject:eachSale];
int eachSaleDay = [eachSale.saleDay intValue];
// if the next day is different from the one before it, add a new section.
if (saleDay != eachSaleDay) {
_saleDaysCount += 1;
NSLog(#"eachSaleDay %i" ,eachSaleDay);
NSLog(#"salesDayCount %i" ,_saleDaysCount);
}
saleDay = eachSaleDay;
}
This will give me the correct number of sections, but how do I group the objects according to the day to find out the number of rows for each section, and what objects should go in each row?
I think I need to use NSSort Descriptors but I'm not sure where I would implement it and how to access the individual day arrays.
Any suggestions?
Thanks
I would simply add an additional field of type NSNumber with the complete date for each Sale while you are looping through your results.
eachSale.saleDateIdentifier = #(eachSale.saleYear.intValue * 10000 +
eachSale.saleMonth.intValue * 100 +
eachSale.saleDay.intValue);
Now it is easy to sort and count:
[_salesList sortUsingDescriptors:
#[[NSSortDescriptor sortDescriptorWithKey:#"saleDateIdentifier" ascending:NO]]];
NSArray *allDates = [_salesList valueForKeyPath:#"saleDateIdentifier"];
NSSet *uniqueDates = [NSSet setWithArray:allDates];
NSArray *sortedUniqueDates = [uniqueDates sortedArrayUsingDescriptors:
#[[NSSortDescriptor sortDescriptorWithKey:#"saleDateIdentifier" ascending:NO]]];
Your number of sections is sortedUniqueDates.count. For each section you could use the saleDateIdentifier to format the date string in titleForRowAtIndexPath.
BTW, maybe you do not want to use valueForKey in your init method for eachSale. Maybe you actually mean objectForKey. Better even, you should type the properties of the Sale class and use NSNumber and NSString as appropriate.
I got this json response
{
"generated_in" = "0.0283";
stat = ok;
videos = {
"on_this_page" = 3;
page = 1;
perpage = 50;
total = 3;
video = (
{
"embed_privacy" = anywhere;
id = 73198189;
"is_hd" = 1;
"is_watchlater" = 0;
license = 0;
"modified_date" = "2013-08-27 01:29:16";
owner = 20303618;
privacy = anybody;
title = Untitled;
"upload_date" = "2013-08-27 00:57:36";
},
{
"embed_privacy" = anywhere;
id = 73197989;
"is_hd" = 0;
"is_watchlater" = 0;
license = 0;
"modified_date" = "2013-08-27 01:24:17";
owner = 20303618;
privacy = anybody;
title = sample2;
"upload_date" = "2013-08-27 00:52:40";
},
{
"embed_privacy" = anywhere;
id = 72961770;
"is_hd" = 0;
"is_watchlater" = 0;
license = 0;
"modified_date" = "2013-08-23 05:57:48";
owner = 20303618;
privacy = anybody;
title = sample;
"upload_date" = "2013-08-23 05:25:44";
}
);
};
}
when i am trying to parse it for the video id.
The technique i used is
i converted that json into NSDictionary jsondata and
NSString *videoid = [[[jsondata objectForKey:#"videos"]valueForKey:#"video"]valueForKey:#"id"];
NSLog(#"video string is %#",videoid);
the result is:
(
73198189,
73197989,
72961770
)
but i am not able to access the normal string functions on that string to retrieve the id's.
it is saying an error unrecognized selector was sent.
Is there a better way to parse that string, i tried google but every post says the same approach.
You not getting a string but an array of strings, the JSON response holds mutiple videos. The code you used will retrieve all the id of all the videos.
You can loop thru the found videos like:
NSArray *videoIdArray = [jsondata valueForKeyPath:#"videos.video.id"];
for(NSString *videoId in videoIdArray) {
NSLog(#"video string is %#",videoId);
}