Skip to content

Instantly share code, notes, and snippets.

@doluvor
doluvor / GetFileInTestBundle
Created December 6, 2016 07:40
Get file in test bundle
let testBundle = Bundle(for: type(of: self))
let fileURL = bundle.url(forResource: "imageName", withExtension: "png")
@doluvor
doluvor / DeleteDuplicated.rb
Created October 24, 2016 06:20
Delete duplicated records in Rails
Product.group_by{|x| [x.legacy_id, x.company]}
.flat_map{|_,x| x.drop(1)}
.delete_all
Product.where.not(id: Product.group(:legacy_id, :company).pluck('min(products.id)'))
.delete_all
delete from products
where id not in (
select min(p.id) from products p group by p.legacy_id, p.company
@doluvor
doluvor / LoadViewFromNib.m
Created October 21, 2016 02:44
Load view from nib
MyViewClass* myViewObject = [[[NSBundle mainBundle] loadNibNamed:@"MyViewClassNib" owner:self options:nil] objectAtIndex:0]
@doluvor
doluvor / Thumbnail.m
Created October 18, 2016 03:34
Generate thumbnail of video
AVAsset *asset = [AVAsset assetWithURL:url];
CMTime duration = [asset duration];
CMTime snapshot = CMTimeMake(duration.value * progress, duration.timescale);
AVAssetImageGenerator *generator = [AVAssetImageGenerator assetImageGeneratorWithAsset:asset];
CGImageRef imageRef = [generator copyCGImageAtTime:snapshot actualTime:nil error:nil];
UIImage *thumbnail = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
@doluvor
doluvor / TransparentUIToolbarUINavigationBar.m
Created October 17, 2016 06:34
Transparent UIToolbar and UINavigationBar
[self.toolbar setBackgroundImage:[UIImage new]
forToolbarPosition:UIBarPositionAny
barMetrics:UIBarMetricsDefault];
[self.toolbar setShadowImage:[UIImage new]
forToolbarPosition:UIBarPositionAny];
[self.navigationBar setBackgroundImage:[UIImage new]
forBarMetrics:UIBarMetricsDefault];
self.navigationBar.shadowImage = [UIImage new];
self.navigationBar.translucent = YES;
@doluvor
doluvor / ObserveNetworkChange.m
Created October 17, 2016 05:53
Observe iOS network change
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), //center
NULL, // observer
onNotifyCallback, // callback
CFSTR("com.apple.system.config.network_change"), // event name
NULL, // object
CFNotificationSuspensionBehaviorDeliverImmediately);
static void onNotifyCallback(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo)
{
NSString* notifyName = (NSString*)name;
@doluvor
doluvor / SetRealmForUser.swift
Created October 1, 2016 13:50
Set default Realm for user
func setDefaultRealmForUser(username: String) {
var config = Realm.Configuration()
config.fileURL = config.fileURL!.deletingLastPathComponent().appendingPathComponent("\(username).realm")
Realm.Configuration.defaultConfiguration = config
}
@doluvor
doluvor / SingletonUsingGCD.m
Created September 29, 2016 15:20
Singleton using GCD
+ (instancetype)sharedInstance {
static dispatch_once_t once;
static id instance;
dispatch_once(&once, ^{
instance = [self new];
});
return instance;
}