Query Amazon DynamoDB for specific result - ios

I am using the DynamoDB SDK v2 . I can scan the database however I am struggling to query it for a specific results. I don't know if I have the DB setup wrong or I am doing the query wrong.
When I use
- (void)getTableRow:(NSString *)rangeKey {
AWSDynamoDBObjectMapper *dynamoDBObjectMapper = [AWSDynamoDBObjectMapper defaultDynamoDBObjectMapper];
[[dynamoDBObjectMapper load:[SonicUserTableRow class]
hashKey:#"UserId"
rangeKey:rangeKey] continueWithExecutor:[BFExecutor mainThreadExecutor] withBlock:^id(BFTask *task) {
if (!task.error) {
SonicUserTableRow *tableRow = task.result;
NSLog (#"Table Row %#", tableRow);
} else {
//Catch Error
}
return nil;
}];
}
I am calling it using [self getTableRow:#" ENTER UNIQUE USER ID "];
However I am getting the error
The provided key element does not match the schema
How can I fix this?
Note I am using the new Amazon SDK v2.
My Database
HASH & RANGE KEYS
SonicUserTableRow .m
+ (NSString *)dynamoDBTableName {
return #"SonicUsers";
}
+ (NSString *)hashKeyAttribute {
return #"UserId";
}
+ (NSString *)rangeKeyAttribute {
return #"Username";
}
SonicUserTableRow .h
#interface SonicUserTableRow : AWSDynamoDBModel <AWSDynamoDBModeling>
#property (nonatomic, strong) NSString *UserId;
#property (nonatomic, strong) NSString *Username;
#property (nonatomic, strong) NSString *profileImage;
#property (nonatomic, strong) NSString *active;
#property (nonatomic, strong) NSString *email;
#property (nonatomic, strong) NSString *flags;
#property (nonatomic, strong) NSString *attuned;
I have UserId value UserIdString: us-east-1:4ae0a93a-59a6-45fd-80c8-ff6b99f1d152 in the database so am trying to call for example: [self getTableRow:#"us-east-1:4ae0a93a-59a6-45fd-80c8-ff6b99f1d152"];

If DropId is a hash key of your table, you can use - load:hashKey:rangeKey: to load a single item.

Related

Read data from node inside a node Realm iOS

I'm pretty new to Realm and I'm trying to integrate Realm with Firebase. I'm fetching data using realm using a predicate.
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"isArchived == NO AND userId == %#", [User userId]];
RLMResults *dbrecents = [[Recent objectsWithPredicate:predicate] sortedResultsUsingProperty:FRECENT_LASTMESSAGEDATE ascending:NO];
and my firebase data structure,
my Recent object has the exact same values like in firebase structure,
#property NSString *objectId;
#property NSString *userId;
#property NSString *groupId;
#property NSString *initials;
#property NSString *picture;
#property NSString *description;
#property NSString *members;
#property NSString *password;
#property NSString *type;
#property NSString *fullname;
#property NSString *senderName;
#property NSInteger counter;
#property NSString *lastMessage;
#property NSTimeInterval lastMessageDate;
#property BOOL isArchived;
#property BOOL isDeleted;
#property BOOL message_me;
#property NSTimeInterval createdAt;
#property NSTimeInterval updatedAt;
When I'm retrieving data, the dbrecents is empty. How may I fix this?

How do I pass an array of objects from iPhone to Apple Watch. Only certain fields should be passed

I have a Model class 'Employee'. It has the following fields:
#interface Employee : NSManagedObject
#property (nonatomic, retain) NSNumber * employeeID;
#property (nonatomic, retain) NSString * name;
#property (nonatomic, retain) NSNumber * age;
#property (nonatomic, retain) NSString * address;
#property (nonatomic, retain) NSString * designation;
#property (nonatomic, retain) NSString * teamName;
#property (nonatomic, retain) NSString * gender;
#property (nonatomic, retain) NSNumber * dateOfJoining;
#end
I need to pass an array of 'Employee' to the Watch app but only three fields : name, gender, designation. How do I do this? Should I create a new Model class which has only this three fields and share it between iPhone and Watch?
Eg:
#interface EmployeeData : NSManagedObject
#property (nonatomic, retain) NSString * name;
#property (nonatomic, retain) NSString * designation;
#property (nonatomic, retain) NSString * gender;
#end
And then should I serialise array of EmployeeData and send it as NSData to the watch?
You can send data inside a dictionary as well. Although it cannot be your specific class, you can pass the NSString class for your name, designation, and gender. Possibly share common string keys for your dictionary with both your watch extension and your phone App so the watch knows the string keys to use.
WCSession Documentation
Watch: Asking for data from the watch when you need it with WCSession sendMessage:replyHandler. Sends a message to the phone app and receives its callback.
[[WCSession defaultSession] sendMessage:#{#"EmployeData": #(YES)} replyHandler:^(NSDictionary<NSString *,id> * _Nonnull replyMessage) {
// Extract data
} errorHandler:^(NSError * _Nonnull error) {
// Failure to reach phone
}];
iPhone: A class must conform to WCSessionDelegate protocol. session:didReceiveMessage: receives message from watch and replies with a dictionary of data. Here you can supply your information in the form of a dictionary.
- (void)session:(WCSession *)session didReceiveMessage:(NSDictionary<NSString *,id> *)message replyHandler:(void (^)(NSDictionary<NSString *,id> * _Nonnull))replyHandler
{
// Your case could be:
NSDictionary * aDictionary = #{#"Employees" : #[ #{#"name" : #"Alfred", #"designation" : #"Alabama", #"gender": #"Male"}, #{#"name" : #"Joe", #"designation" : #"New York", #"gender" : #"Male"} ]};
replyHandler(aDictionary);
}

Mantle modelOfClass json parsing

I have the following Mantle object & want to convert the dictionary to Mantle object. but the conversion always returns empty MTLModel object.
Any ideas?
//ABC.h
#interface ABC : MTLModel<MTLJSONSerializing>
#property (nonatomic, strong) NSString *Identifier;
#property (nonatomic, strong) NSString *Name;
#property (nonatomic, strong) NSString *ImageURLString;
#property (nonatomic, strong) NSString *Desc;
#property (nonatomic, strong) NSString *lastUpdated;
#end
// ABC.m
#implementation ABC
+ (NSDictionary *)JSONKeyPathsByPropertyKey {
return #{
#"Identifier" : #"id",
#"Name" : #"name",
#"Desc" : #"desc",
#"ImageURLString" : #"image_url",
#"lastUpdated" : #"last_updated_at"
};
}
#end
Calling code:
ABC *abc = [MTLJSONAdapter modelOfClass:ABC.class fromJSONDictionary:tempDict error:NULL];
The JSON dictionary is:
{
desc = "asdadasdakjqewqwmsdnasdaksdasd";
id = adsasdasdasdasd;
"image_url" = "http://upload.wikimedia.org/wikipedia/commons/thumb/1/19/abc.JPG";
"last_updated_at" = "2015-07-25 04:22:39.851710";
name = asdasdqwe;
}
The result ABC object has no contents. Can't figure what I am missing here.
Any ideas???
Try with this snippet and debug the adapterError. May have relevant information about the conversion from NSDictionary to Mantle object
//Create Mantle object from NSDictionary using MTLJSONSerialization
NSError *adapterError;
MTLModel *user = [MTLJSONAdapter modelOfClass:MTLModel.class fromJSONDictionary:dictionary error:&adapterError];
if(adapterError) {
PrintError(adapterError)
return nil;
}
id = adsasdasdasdasd;
needs to be
id = "adsasdasdasdasd";

RKMappingResult has objects, but they NSLog as null

What I'm doing
Using RestKit, I'm making a GET request to get a JSON object that contains an array of User objects that populate a UITableView. I pass that array into a private NSArray called users which becomes _users (I'm still fuzzy on this). This works, and the table populates fine. I can access the individual objects in the _users array from my other methods, such as [UITableViewCell cellForRowAtIndex].
However, at the same time I pull the data down, and before I call [self.tableView reloadData] from inside the success block of [RKObjectManager getObjectsAtPath...], I want to process the individual objects a little bit.
My problem
Using [RKObjectManager getObjectsAtPath parameters success failure], success returns the RKMappingResult as expected, and I pass its array to a _users, which populates my UITableView. This works, but in the same success block, I try NSLog'ing _users[i] and it returns *nil description*. I know the values are being set at some point, because I populate my UITableViewCells by calling _users[i] in another method.
Hopefully more helpful info
When I NSLog(#"%#", _users) from inside the success block, and know for a fact there are 3 objects in the array, I see:
(
(null),
(null),
(null)
).
I can provide more info, I'm just not sure what to put. I can also show my code, but it's basically out of the book from the RestKit docs.
User object
#interface User : NSObject
#property (nonatomic, strong) NSString *id;
#property (nonatomic, strong) NSString *email;
#property (nonatomic, strong) NSString *username;
#property (nonatomic, strong) NSString *fullName;
#property (nonatomic, strong) NSString *bio;
#property (nonatomic) NSDate *dob;
#property (nonatomic, strong) NSString *avatar;
#property (nonatomic, strong) NSString *avatarMeta;
#property (nonatomic, strong) NSString *location;
#property (nonatomic, strong) NSURL *url;
#property (nonatomic, strong) NSString *enabled;
#end
RKObjectManager example
*note - some pieces have been removed for security reasons
- (void)loadUsers {
NSMutableDictionary *params = [[NSMutableDictionary alloc] initWithDictionary:#{#"token": self.token}];
NSString *path = #"/api/v1/g/feed";
if ( self.cursor ) {
path = [NSString stringWithFormat:#"%#/%#", path, self.cursor];
}
[[RKObjectManager sharedManager] getObjectsAtPath:path
parameters:params
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
_users = mappingResult.array;
NSLog(#"Last User: %#",_users[0]); // *nil description*
NSLog(#"Array: %#", _users); // ((null),(null),(null),(null),(null),(null),(null),(null),(null),(null))
[self.tableView reloadData]; // table gets populated correctly
}
failure:^(RKObjectRequestOperation *operation, NSError *error) {
NSLog(#"No feed available: %#", error);
}];
}
Most likely you're overriding the description method, probably by adding your own property with that name, in the user class and setting that to an empty string. This breaks everything. You should rename that property to overview or something like that and update your usage.

Initialization of custom object return null [duplicate]

This question already has an answer here:
why is my code outputting *nil description*
(1 answer)
Closed 10 years ago.
Here is my custom class:
ClassA.h
#interface ClassA : NSObject<RKRequestDelegate>{
NSString *uri;
NSString *folderUri;
NSInteger idFolder;
NSString *kind;
bool isMine;
CustomUser *owner;
NSMutableArray *usersAdministrators;
NSMutableArray *usersContributors;
NSMutableArray *usersReaders;
NSString *visibility;
NSString *name;
NSString *description;
NSMutableArray *comments;
}
#property (nonatomic,copy) NSString *uri;
#property (nonatomic,copy) NSString *folderUri;
#property (nonatomic,assign) NSInteger idFolder;
#property (nonatomic,copy) NSString *kind;
#property (nonatomic,assign) bool isMine;
#property (retain) DMIUser *owner;
#property (nonatomic,copy) NSMutableArray *usersAdministrators;
#property (nonatomic,copy) NSMutableArray *usersContributors;
#property (nonatomic,copy) NSMutableArray *usersReaders;
#property (nonatomic,copy) NSString *visibility;
#property (nonatomic,copy) NSString *name;
#property (nonatomic,copy) NSString *description;
#property (nonatomic,copy) NSMutableArray *comments;
#end
ClassA.m
#implementation ClassA
#synthesize uri,folderUri,idFolder,kind,isMine,owner,usersAdministrators,usersContributors,usersReaders,visibility,name,description,comments;
-(NSString*)description {
return #"ClassA";
}
#end
Quite simple. But when i try to create new instance of this, like this:
datas = [NSMutableArray array]; // Tried with [[NSMutableArray alloc] init] => same thing
ClassA *classA = [[ClassA alloc] init];
[datas addObject:classA];
NSLog(#"classA = %#",classA);
NSLog(#"datas = %#",datas);
First NSLog returns "ClassA".
Second NSLog returns "datas = ()"
What's wrong here? I always created class like this and i've never had problem like this.
Thanks!
Ok guyzzz i found the problem. It's my attribute:
NSString *description;
Seems that iOs doesn't love that. It conflict with the -description method in NSObject...
After that, i found a similar question here:
Why can't I use "description" as an attribute name for a Core Data entity?
Cheers
if your want to return some value implement method description in the ClassA
- (NSString *)description {
return #"ClassA";
}
You've got everything you need. Are you sure the variable you're assigning to isn't a weak one? All delegate properties are weak, therefore not retaining the object. My guess is that you're doing something like this
someObject.delegate = [[ClassA alloc] init];
NSLog(#"%#", someObject.delegate);
Because the delegate property is weak it doesn't hold onto the variable.
Edit:
This whole answer assumes you are using ARC. If not, disregard.

Resources