I am using
-(void)updateHeartRate:(NSArray<__kindof HKSample *> *)samples
to retrieve the HearteRate from the internal watch sensor. Depending on the time the app is in background (deactivated) several heartRateSamples must be retrieved using:
if (samples.count>0) {
for (HKQuantitySample *heartRateSample in samples) {
dispatch_async(dispatch_get_main_queue(), ^{
if (heartRateSample) {
NSLog(#"HEART RATE: %#", [#([heartRateSample.quantity doubleValueForUnit:heartRateUnit]) stringValue]);
}
});
}
}
but how can I retrieve the date/time when the specific sample was written to the HealthKit?
The quantity sample's startDate and endDate properties describe when the sample was taken:
HK_CLASS_AVAILABLE_IOS(8_0)
#interface HKSample : HKObject
#property (readonly, strong) HKSampleType *sampleType;
#property (readonly, strong) NSDate *startDate;
#property (readonly, strong) NSDate *endDate;
#end
Related
Does Realm support UIColors?
How does one go about adding a UIColor property to a subclass of RLMObject, and what is the recommended method for doing so?
Thank you for the help!
Realm does not directly support reading and writing UIColor objects.
That being said, it should be relatively easy to convert a UIColor to a format that can be saved to Realm, and then convert it back again on demand.
There's no officially recommended way, but the way I would recommend is to convert the UIColor to its hexadecimal version, and save it as a string to the Realm object.
There are many libraries on GitHub for performing UIColor to hexadecimal conversions, such as this one: https://github.com/nicklockwood/ColorUtils
Good luck!
I created a superclass which allows any NSObject that supports NSCoding to be saved as a Data object in realm.
class RLMArchiveableObject : Object {
dynamic var data: Data?
var object: Any? {
get {
return (data == nil) ? nil : NSKeyedUnarchiver.unarchiveObject(with: data!)
}
set {
data = (newValue == nil) ? nil : NSKeyedArchiver.archivedData(withRootObject: newValue!)
}
}
override class func ignoredProperties() -> [String] {
return ["object"]
}
}
you just need to convert color to string and save that string to realm
extension UIColor {
func toString() -> String {
let colorRef = self.cgColor
return CIColor(cgColor: colorRef).stringRepresentation
}
}
I was able to achieve the desired result by adding three float values to the subclassed RLMObject representing the RGB values of the UIColor.
For those having similar problems, I have included my solution below.
Interface File
#interface LFKColor : RLMObject
#property NSString *name;
#property float redColor;
#property float blueColor;
#property float greenColor;
#property (nonatomic, strong) UIColor *color;
#end
Implementation File
#implementation LFKColor
#synthesize color;
+ (NSString *)primaryKey {
return #"name";
}
+ (NSArray *)ignoredProperties {
return #[#"color"];
}
-(void)setColor:(UIColor *)selectedColor {
self->color = selectedColor;
const CGFloat* components = CGColorGetComponents(selectedColor.CGColor);
self.redColor = components[0];
self.greenColor = components[1];
self.blueColor = components[2];
}
-(UIColor *)color {
return [[UIColor alloc] initWithRed:self.redColor green:self.greenColor blue:self.blueColor alpha:1.0];
}
#end
I'm trying to use a subclass of a RLMObject as a base class for a property of other subclass of RLMObject, with no luck. Is this a known restriction, or is there a workaround for it?
test.m:
#import <XCTest/XCTest.h>
#import <Realm/Realm.h>
#interface Base : RLMObject; #end
#interface Derived : Base; #end
#interface User : RLMObject
#property Base *obj;
#end
#implementation Base; #end
#implementation Derived; #end
#implementation User; #end
#interface rlmrlmTests : XCTestCase; #end
#implementation rlmrlmTests
- (void) testOK {
User *user = [[User alloc] init];
user.obj = [[Base alloc] init]; // OK
[[RLMRealm defaultRealm] transactionWithBlock:^{
[[RLMRealm defaultRealm] addObject:user];
}];
}
- (void) testZFail {
User *user = [[User alloc] init];
user.obj = [[Derived alloc] init]; // ERROR
[[RLMRealm defaultRealm] transactionWithBlock:^{
[[RLMRealm defaultRealm] addObject:user];
}];
}
#end
will get an exception saying:
error: -[rlmrlmTests testZFail] : failed: caught "RLMException", "Can't set object of type 'Derived' to property of type 'Base'"
That's a current limitation of Realm. It doesn't support polymorphism on relations.
If possible, I'd avoid inheritance in this case. You could reach that by altering your object-relational mapping: You could add a further discriminator column to your Base class and have relations to classes which contain further attributes and wouldn't inherit anymore from the Base class. If you have just one subclass, then a discriminator column is eventually not needed.
#interface Base : RLMObject
#property NSString *discriminator;
#property BaseExtensionA *extA;
#property BaseExtensionB *extB;
#end
#interface BaseExtensionA : RLMObject
// the properties of the prior `Derived` would go here
#end
#interface BaseExtensionB : RLMObject
// …
#end
I'm trying to save a list of assets to upload in a sqllite3 db, but when i parse the database and set the assets to an array, then try to use the asset i get a SIGABRT error.
ALAsset *asset = (ALAsset *) assets[indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:#"image%d: ready to upload.",indexPath.row];
cell.detailTextLabel.text = #"1.3MB to folder <server folder>";
[[cell imageView] setImage:[UIImage imageWithCGImage:[asset thumbnail]]];// SIGABRT ERROR
Im saving the ALAsset to the database as a string (TEXT) with UTF8formatting
NSMutableArray *tmpArray = [NSMutableArray alloc]init];
///get sql
[tmpArray addObject:someStringFromSQL];
///end sql loop
assets = [tmpArray mutableCopy];
in the code above I tried:
[[cell imageView] setImage:[UIImage imageWithCGImage:[(ALAsset *) asset thumbnail]]];// SIGABRT ERROR
and that didn't work.
This is the error:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString thumbnail]: unrecognized selector sent to instance 0xc0a7800'
Any suggestions?
Also as a side question: Does anyone know how to get the file size (i.e. 1.3MB) from the asset?
BLOCK:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
//do stuff in cell
NSURL *aURL =[NSURL URLWithString:[assets objectAtIndex:indexPath.row]];
[assetsLibrary assetForURL:aURL resultBlock:^(ALAsset *asset){
dispatch_async(dispatch_get_main_queue(), ^{
cell.imageView.image = [UIImage imageWithCGImage:[asset thumbnail]];
});
[[NSNotificationCenter defaultCenter] postNotificationName:#"newAssetImageRetrieved" object:nil];
//in this notificaton I'm reloading the data; its putting the tableview in an infinite loop - but the images display...
}
failureBlock:^(NSError *error){
// error handling
NSLog(#"Can't get to assets: FAILED!");
}];
//cell.imageView.image = [UIImage imageWithCGImage:[asset thumbnail]];
cell.textLabel.text = [NSString stringWithFormat:#"image%d: ready to upload.",indexPath.row];
cell.detailTextLabel.text = [NSString stringWithFormat:#"1.3MB to folder %#", [destinations objectAtIndex:indexPath.row]];
//[[cell imageView] setImage:[UIImage imageWithCGImage:[asset thumbnail]]];
return cell;
}
There are a couple of issues with your code sample:
The image retrieval is happening asynchronously, so when you try to update the image, you want to make sure the cell is still visible (and not reused for another NSIndexPath).
In this case, the retrieval from the ALAssetsLibrary will probably be so fast that this isn't critical, but it's a good pattern to familiarize yourself with, because if you're ever retrieving images over the Internet, this issue becomes increasingly important.
Because cells are being reused, if you don't find the image immediately and have to update it asynchronously, make sure you reset the UIImageView before initiating the asynchronous process. Otherwise, you'll see a "flickering" of replacing old images with new ones.
You are using UITableViewCell for your cell. The problem with that is that it will layout the cell based upon the size of the image present by the time cellForRowAtIndexPath finishes.
There are two easy solutions to this. First, you could initialize the cell's imageView to be a placeholder image of the correct size. (I usually have an image called placeholder.png that is all white or all transparent that I add to my project, which is what I used below.) This will ensure that cell will be laid out properly, so that when you asynchronously set the image later, the cell will be laid out properly already.
Second, you could alternatively use a custom cell whose layout is fixed in advance, bypassing this annoyance with the standard UITableViewCell, whose layout is contingent upon the initial image used.
I'd suggest using a NSCache to hold the thumbnails images. That will save you from having to constantly re-retrieve the thumbnail images as you get them from your ALAssetsLibrary as you scroll back and forth. Unfortunately, iOS 7 broke some of the wonderful NSCache memory-pressure logic, so I'd suggest a cache that will respond to memory pressure and purge itself if necessary.
Anyway, putting that all together, you get something like:
#interface ViewController ()
#property (nonatomic, strong) NSMutableArray *assetGroups;
#property (nonatomic, strong) ALAssetsLibrary *library;
#property (nonatomic, strong) ThumbnailCache *imageCache;
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.imageCache = [[ThumbnailCache alloc] init];
self.assetGroups = [NSMutableArray array];
self.library = [[ALAssetsLibrary alloc] init];
[self.library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
if (!group) {
[self.tableView reloadData];
return;
}
CustomAssetGroup *assetGroup = [[CustomAssetGroup alloc] init];
assetGroup.name = [group valueForProperty:ALAssetsGroupPropertyName];
assetGroup.assetURLs = [NSMutableArray array];
[group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
if (result) {
[assetGroup.assetURLs addObject:[result valueForProperty:ALAssetPropertyAssetURL]];
}
}];
[self.assetGroups addObject:assetGroup];
} failureBlock:^(NSError *error) {
NSLog(#"%s: enumerateGroupsWithTypes error: %#", __PRETTY_FUNCTION__, error);
}];
}
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.assetGroups.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
CustomAssetGroup *group = self.assetGroups[section];
return [group.assetURLs count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
CustomAssetGroup *group = self.assetGroups[section];
return group.name;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
// note, these following three lines are unnecessary if you use cell prototype in Interface Builder
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
CustomAssetGroup *group = self.assetGroups[indexPath.section];
NSURL *url = group.assetURLs[indexPath.row];
NSString *key = [url absoluteString];
UIImage *image = [self.imageCache objectForKey:key];
if (image) {
cell.imageView.image = image;
} else {
UIImage *placeholderImage = [UIImage imageNamed:#"placeholder.png"];
cell.imageView.image = placeholderImage; // initialize this to a placeholder image of the right size
[self.library assetForURL:url resultBlock:^(ALAsset *asset) {
UIImage *image = [UIImage imageWithCGImage:asset.thumbnail]; // note, use thumbnail, not fullResolutionImage or anything like that
[self.imageCache setObject:image forKey:key];
// see if the cell is still visible, and if so, update it
// note, do _not_ use `cell` when updating the cell image, but rather `updateCell` as shown below
UITableViewCell *updateCell = (id)[tableView cellForRowAtIndexPath:indexPath]; // not to be confused with similarly named table view controller method ... this one checks to see if cell is still visible
if (updateCell) {
[UIView transitionWithView:updateCell.imageView duration:0.1 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
updateCell.imageView.image = image;
updateCell.textLabel.text = asset.defaultRepresentation.filename;
} completion:nil];
}
} failureBlock:^(NSError *error) {
NSLog(#"%s: assetForURL error: %#", __PRETTY_FUNCTION__, error);
}];
}
return cell;
}
#end
The above uses the following classes:
/** Thumbnail cache
*
* This cache optimizes retrieval of old thumbnails. This purges itself
* upon memory pressure and sets a default countLimit.
*/
#interface ThumbnailCache : NSCache
// nothing needed here
#end
#implementation ThumbnailCache
/** Initialize cell
*
* Add observer for UIApplicationDidReceiveMemoryWarningNotification, so it purges itself under memory pressure
*/
- (instancetype)init
{
self = [super init];
if (self) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(removeAllObjects) name:UIApplicationDidReceiveMemoryWarningNotification object:nil];
self.countLimit = 50;
};
return self;
}
/** Dealloc
*
* Remove observer before removing cache
*/
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidReceiveMemoryWarningNotification object:nil];
}
#end
and
/** Custom AssetGroup object
*
* This is my model object for keeping track of the name of the group and list of asset URLs.
*/
#interface CustomAssetGroup : NSObject
#property (nonatomic, copy) NSString *name;
#property (nonatomic, strong) NSMutableArray *assetURLs;
#end
#implementation CustomAssetGroup
// nothing needed here
#end
You have to explore all the code base related to the save and retrieve functionality.
However, here are some good tips.
Save the ALAsset Url instead of saving the entire ALAsset as a string.
Retrieve the ALAsset Url from the database and convert it to NSUrlString.
Use ALAsset Library to load the image or thumbnail back.
Hope this will help you.
I know that the EKEventStore class has the #property NSArray *calendars that returns an array of an event store's calendar objects, however I do not how to access a specific calendar inside of that array when all I know is the calendar's title. The only examples I can find online are of programs that access the defaultCalendarForNewEvents, but the calendar I want is not the default calendar. I also don't know the calendar's unique identifier, all I know is the title. I tried to use a valueForKey method to access the calendar titled BART, but what I am doing is definitely wrong, can anyone help? Here is what i tried:
#interface BARTClass : NSObject {
EKEventStore *eventStore;
EKCalendar *bart
NSArray *calendars;
}
#property (nonatomic, retain) EKEventStore *eventStore;
#property (nonatomic, retain) NSArray *calendars;
#property (nonatomic, retain) EKCalendar *bart;
-(EKCalendar) getBartCalendar;
#end
...
#implementation
#synthesize calendars, bart, eventStore;
-(EKCalendar) getBartCalendar {
[self setEventStore: [[EKEventStore alloc] init]];
[self setCalendars = [eventStore calendars]];
NSArray *titles = [calendars valueForKey:#"title"];
[self setBart:[titles valueForKey:#"BART"]];
...
you are near to the goal, I think.
You can do it like this:
EKEventStore *eventStore = [[EKEventStore alloc]init];
EKCalendar *bart;
NSArray *myCals = [eventStore calendars];
for (int i=0; i < [myCalls count]; i++) {
bart = [myCals objectAtIndex:i];
if (bart.title isEqualToString:#"Bart"){
break;
}
else {
bart = nil;
}
}
If the calendar "Bart" exists, you get it at the end of the loop.
I have been struggling with this piece of code for a while and I just don't know why it happens that when running in the device ... the app crashes with an EXC_BAD_ACCESS Error but when running in the simulator it runs fine.
The scenario: A subclass of NSOperation that makes an async connection with NSURLConnection and gets custom data. When finished, it calls the block with the downloaded data.
Here is the .h file:
#interface FileDownloader : NSOperation <NSURLConnectionDataDelegate>
typedef void (^CompletionBlockForFile)(NSData *);
- (id)initWithCompletionBlock:(CompletionBlockForFile)block;
#end
and the .m file:
#interface FileDownloader ()
#property (strong, nonatomic) CompletionBlockForFile completionBlock;
#property (strong, nonatomic) NSMutableData *downloadedData;
- (void)downloadFileWithCompletionBlock:(CompletionBlockForFile)block;
#end
#implementation FileDownloader
#synthesize downloadedData = _downloadedData;
#synthesize completionBlock = _completionBlock;
- (id)initWithCompletionBlock:(CompletionBlockForFile)block
{
self = [super init];
if (self) {
_completionBlock = block;
}
return self;
}
- (void)main
{
if (self.isCancelled) return;
if (_completionBlock) {
[self downloadFileWithCompletionBlock:_completionBlock];
}
}
- (void)downloadFileWithCompletionBlock:(CompletionBlockForFile)block
{
NSURL *url = [NSURL URLWithString:#"http://www.google.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
dispatch_async(dispatch_get_main_queue(), ^{
NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
[connection start];
});
}
... delegate methods of NSURLConnection
#end
And Finally, the method that adds the operation object to the queue at the MainViewController Class:
- (void)viewDidLoad
{
[super viewDidLoad];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
FileDownloader *fileDownloader = [[FileDownloader alloc] initWithCompletionBlock:^(NSData *data){ // <----- HERE IS THE EXC_BAD_ACCES ERROR JUST WHEN RUNNING IN THE DEVICE !!! :S
NSLog(#"%#", data);
}];
[queue addOperation:fileDownloader];
}
Can anybody explain me what am I doing wrong? And is it correct to put strong for the block var in the property? Why not assign? Or weak ?
Thanks in advance :)
Blocks should be copied and not retained.
Change
#property (**strong**, nonatomic) CompletionBlockForFile completionBlock;
to
#property (**copy**, nonatomic) CompletionBlockForFile completionBlock;
and it should work