Skip to content

Instantly share code, notes, and snippets.

View mikekatz's full-sized avatar

Michael Katz mikekatz

  • New York, NY
  • 15:13 (UTC -04:00)
View GitHub Profile
@mikekatz
mikekatz / SaveLuaTableToKinvey.lua
Created June 8, 2012 15:50
Save Lua Table to Kinvey - blog example
-- load required modules
http = require("socket.http") --luasocket
ltn12 = require("ltn12")
mime = require("mime")
io = require("io")
json = require("json") -- luajson
-- Create a Lua table to represent our entity to save
--- This is from our doc REST example: http://docs.kinvey.com/rest-appdata.html
jamesBond = { ["firstName"] = "James", ["lastName"] = "Bond", ["email"] = "[email protected]", ["age"] = 34 }
@mikekatz
mikekatz / ReadLuaTableFromKinvey.lua
Created June 8, 2012 15:51
Save Lua Table To Kinvey - blog exmaple 2 - read
-- GET back the object to verify that we saved it
response = {}
save = ltn12.sink.table(response)
id = objAsTable["_id"] -- get the _id field from the object table
url = "https://baas.kinvey.com/appdata/KINVEY_APP_ID/testObjects/" .. id
h = {Authorization = "Basic " .. (mime.b64("KINVEY_APP_ID:KINVEY_APP_SECRET")), ["Content-Type"] = "application/json" }
ok, code, headers = http.request{url = url, headers = h, sink = save, redirect = true, method = "GET"}
@mikekatz
mikekatz / gist:3040629
Created July 3, 2012 15:53
Creating a KCSCachedStore and using it for query
- (id) init
{
if (self = [super init]) {
self.tableValues = [NSArray array];
}
return self;
}
- (void) viewWillAppear:(BOOL)animated
{
@mikekatz
mikekatz / gist:3722148
Created September 14, 2012 14:14
Kinvey Supply Mapping for relationship
//books.h
@interface Book : NSObject <KCSPersistable>
@property (nonatomic) Author* author;
@property (nonatomic) NSString* title;
@end
//books.m
@implementation Book
+(NSDictionary *)kinveyPropertyToCollectionMapping {
return @{@"author" : @"authors"};
@mikekatz
mikekatz / gist:3722237
Created September 14, 2012 14:28
Kinvey fetch entity with relationships
KCSCollection* books = [KCSCollection collectionFromString:@"books" ofClass:[Book class]];
KCSLinkedAppdataStore* store = [KCSLinkedAppdataStore storeWithCollection:books options:nil];
[store loadObjectWithId:@"HPBook6" withCompletionBlock:^(NSArray *objectsOrNil, NSError *errorOrNil) {
Book* harryPotter = [objectsOrNil objectAtIndex:0];
Author* jRowling = harryPotter.author;
} withProgressBlock:^(NSArray *objects, double percentComplete) {
//show progress
}];
@mikekatz
mikekatz / gist:4550430
Created January 16, 2013 20:10
Example of writing an OCUnit test that uses asynchronous methods by polling for completion
#define POLL_INTERVAL 0.05 //50ms
#define N_SEC_TO_POLL 1.0 //poll for 1s
#define MAX_POLL_COUNT N_SEC_TO_POLL / POLL_INTERVAL
- (void) testAnAsynchronousFunction
{
__block BOOL done = NO;
//do async on a delay
float delayInSeconds = 0.5;
@mikekatz
mikekatz / gist:4550441
Last active December 11, 2015 05:09
Test asynchronous code with OCUnit, this time with a the polling code in a macro
#define POLL_INTERVAL 0.05 //50ms
#define N_SEC_TO_POLL 1.0 //poll for 1s
#define MAX_POLL_COUNT N_SEC_TO_POLL / POLL_INTERVAL
#define CAT(x, y) x ## y
#define TOKCAT(x, y) CAT(x, y)
#define __pollCountVar TOKCAT(__pollCount,__LINE__)
#define POLL(__done) \
NSUInteger __pollCountVar = 0; \
@mikekatz
mikekatz / gist:4708775
Created February 4, 2013 19:05
Create a simple colored square image. From my UI utils repo https://github.com/mikekatz/iOS-UI-Utils
+ (UIImage*) imageWithColor:(UIColor*)color size:(CGSize)size
{
UIGraphicsBeginImageContext(size);
UIBezierPath* rPath = [UIBezierPath bezierPathWithRect:CGRectMake(0., 0., size.width, size.height)];
[color setFill];
[rPath fill];
UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
@mikekatz
mikekatz / gist:5187639
Created March 18, 2013 14:43
Log all available fonts in iOS
for (NSString* familyName in [UIFont familyNames]) {
NSLog(@"%@",[UIFont fontNamesForFamilyName:familyName]);
}
@mikekatz
mikekatz / gist:5224025
Created March 22, 2013 19:27
Debugging Log macros from a PCH of a recent app. Note that it uses TFLog to log to testFlight, so this is still for in-test, not app store release.
#if DEBUG
#define AssertLog(condition, desc, args...) NSAssert(condition, desc, ## args)
#define DBLog(format, args...) TFLog(@"%s, line %d: " format "\n", __func__, __LINE__, ## args);
#else
#define AssertLog(c, desc, args...) if (!(c)) TFLog(@"%s, line %d: " desc "\n", __func__, __LINE__, ## args);
#define DBLog(format, args...) do {} while(0)
#endif