Re: Memory Warnings

2014-10-08 Thread Quincey Morris
(reposted after editing for length)

On Oct 7, 2014, at 22:12 , Gerriet M. Denkmann gerr...@mdenkmann.de wrote:

 Is there a way for the app to find out how much memory it is currently using?

Here’s my take on this, which may be way off base:

There’s really no practical way to answer such questions any more. The way 
memory is managed is so varied, so diffused that even if you could get an 
answer, it would be as likely to lead you astray as to help you.

In practice, I think the question is not how much memory your app is using, but 
how fast your memory use is increasing over time.

If it’s not increasing over time — and I’m speaking in the most general way 
possible here, so using more memory per opened document isn’t what I’d call 
increasing over time, in this context — then you have a fairly easy evaluation. 
Your app fits (in the foreground) or it crashes. Your app retreats gracefully 
(to the background) or it’s terminated.

The way we program now, we generally regard increasing memory use over time as 
a memory leak, and we fix our code so this doesn’t happen and usage is stable. 
So, it fits or it crashes.

 When I have a file of 100 KB and do:
   myData = [NSData dataWithContentsOfFile:options:error: ];
   myBytes = myData.bytes;
 how much Ram am I now using? 100 KB or 200 KB? I.e. does bytes just return 
 a pointer to some internal structure in NSData?
 
 Would options = NSDataReadingMappedIfSafe be of some help?

The general answer is that you don’t know how much it’s using. In practice, the 
answer used to be 100KB (“bytes” returned an interior pointer). But this isn’t 
always true any more. GCD data blocks, which can be bridged to NSData, can come 
in pieces, and in that case “bytes” may copy the data into contiguous memory.

Mapped data might help in common cases, but how does this save you from “it 
fits or it crashes” when the file turns out to be on a non-local file system, 
on which mapping doesn’t work? Memory mapping is more about performance than 
size, perhaps.

   static dispatch_once_t justOnce;
   dispatch_once(  justOnce, ^{ _myData = [NSData 
 dataWithContentsOfFile:options:error: ]; } );

 If I would (upon receiving a memory warning) release myData, I would never be 
 able to get it back, would I?
 
 How could I handle this case?

Given that ‘dispatch_once’ is intended to do something only once, it’s hardly a 
shock that it doesn’t help in this case. Anyway, the problem is not in keeping 
a single but discardable copy of the data, it’s in making the discard/recovery 
process thread safe.

You can easily use a construct like GCD semaphores to ensure the integrity of 
the data recovery after discarding, but you have to be careful to avoid 
deadlocks. Or, you can easily use a construct like GCD serial queues (perhaps 
even the main queue) as a deadlock-free of getting to the same result, but you 
might have to redesign your code for asynchronicity with regard to the queuing.

Once you’re in the deeps of thread safety, I’m pretty sure there are no more 
free lunches.


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Memory Warnings

2014-10-08 Thread Gerriet M. Denkmann

On 8 Oct 2014, at 13:42, Quincey Morris quinceymor...@rivergatesoftware.com 
wrote:

 (reposted after editing for length)
 
 On Oct 7, 2014, at 22:12 , Gerriet M. Denkmann gerr...@mdenkmann.de wrote:
 
 Is there a way for the app to find out how much memory it is currently using?
 
 Here’s my take on this, which may be way off base:
 
 There’s really no practical way to answer such questions any more. The way 
 memory is managed is so varied, so diffused that even if you could get an 
 answer, it would be as likely to lead you astray as to help you.
 
 In practice, I think the question is not how much memory your app is using, 
 but how fast your memory use is increasing over time.
 
 If it’s not increasing over time 
It's not. Checked also with Leaks Instrument.
  — then you have a fairly easy evaluation. Your app fits (in the foreground) 
 or it crashes. Your app retreats gracefully (to the background) or it’s 
 terminated.
It runs in the foreground. But when in background it sometimes gets terminated.

 
 When I have a file of 100 KB and do:
  myData = [NSData dataWithContentsOfFile:options:error: ];
  myBytes = myData.bytes;
 how much Ram am I now using? 100 KB or 200 KB? I.e. does bytes just return 
 a pointer to some internal structure in NSData?
 
 Would options = NSDataReadingMappedIfSafe be of some help?
 
 The general answer is that you don’t know how much it’s using. In practice, 
 the answer used to be 100KB (“bytes” returned an interior pointer). But this 
 isn’t always true any more. GCD data blocks, which can be bridged to NSData, 
 can come in pieces, and in that case “bytes” may copy the data into 
 contiguous memory.
 
 Mapped data might help in common cases, but how does this save you from “it 
 fits or it crashes” when the file turns out to be on a non-local file system, 
 on which mapping doesn’t work? Memory mapping is more about performance than 
 size, perhaps.

I made a small iOS app which has just 3 buttons: 
Data (loads NSData from file, which resides in the app bundle)
Bytes (which uses myData.bytes)
Count (which sums all bytes in myData)

Result with NSDataReadingMappedIfSafe, Persistent Bytes in Instruments, 
Allocations:
No change with any button (always ca. 3.5 MB).
Data button takes 1 msec, Bytes 73 μsec, first Count takes 250 msec longer than 
subsequent Counts.

Without NSDataReadingMappedIfSafe:
Data button increases Ram use by 15 MB (about size of file); takes 250 msec.
Bytes, Count: no change. Bytes takes 13 μsec. All Counts take same time (ca. 50 
msec).

So NSDataReadingMappedIfSafe seems to be a good idea (at least in my case of a 
fixed file in the app bundle).


 You can easily use a construct like GCD semaphores...

Sounds too much work for now. Maybe to be considered later. Thanks for your 
ideas though.


Kind regards,

Gerriet.


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Responsive scrolling: prepareContentInRect: not called

2014-10-08 Thread Antonio Nunes
On 08 Oct 2014, at 01:41, Ken Thomases k...@codeweavers.com wrote:

 Wondering if anyone has an idea of why prepareContentInRect: is not called 
 for a view when responsive scrolling is enabled.
 
 We are using an NSScrollView subclass that overrides prepareContentInRect:.
 
 I would not expect -prepareContentInRect: to be called on the scroll view.  
 It would be called on the document view (and its subviews?).  The purpose is 
 to allow the document view to prepare itself for the requests to draw the 
 overdraw region.  The scroll view itself is not drawn in the overdraw region. 
  Likewise, I would not expect the scroll view's -drawRect: to be called for 
 overdrawing.

Thanks for your suggestions Ken,

Indeed we do not ‘drawRect:’ anything in the scroll view. :-) 

 (I have tried overriding prepareContentInRect: in the NSScrollView’s 
 subclass, as well as in the view that serves as the scroll view’s document 
 view. Neither of these methods gets called though.)
 
 I would double-check that, in the occasions when you overrode 
 -prepareContentInRect: in the document view, you didn't also accidentally 
 disable responsive scrolling.  Also, make sure your override calls through to 
 super and doesn't unintentionally stop AppKit from further extending the 
 overdraw region by passing the same rect in successive calls.

Turns out the problem is a different, and of course, almost embarrassingly 
silly one. I misspelled the method name: prepareContentInrect rather than 
prepareContentInRect. Now that I’ve caught that and corrected it, the method is 
called. Thanks for you effort anyway.

-António
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

set maximum file size

2014-10-08 Thread 2551
Hi folks

I have an app which needs to save some of its data into a log file. I have the 
method set up already for creating and appending the log file. However, I'd 
like to limit the file size and initiate a file turn over when it reaches a 
certain size. What is the best way to accomplish this?

I'm aware of Cocoa Lumberjack, but I really don't want to import an entire 3rd 
party framework for the sake of the one simple function that I need if I can 
avoid it.

Any thoughts much appreciated.


Best


P
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: set maximum file size

2014-10-08 Thread Alex Zavatone
Why not check the file size before writing?  If the file size  your max, then 
get the length of the message you are about to write and trim that many chars 
from the front of the file before writing.

Or append the message to your file and after writing, check the file size and 
trim the surplus chats from the beginning.

Do those suggestions work for you?

Sent from my iPod Touch. Please pardon typos.

On Oct 8, 2014, at 10:01 AM, 2551 2551p...@gmail.com wrote:

 Hi folks
 
 I have an app which needs to save some of its data into a log file. I have 
 the method set up already for creating and appending the log file. However, 
 I'd like to limit the file size and initiate a file turn over when it reaches 
 a certain size. What is the best way to accomplish this?
 
 I'm aware of Cocoa Lumberjack, but I really don't want to import an entire 
 3rd party framework for the sake of the one simple function that I need if I 
 can avoid it.
 
 Any thoughts much appreciated.
 
 
 Best
 
 
 P
 ___
 
 Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
 
 Please do not post admin requests or moderator comments to the list.
 Contact the moderators at cocoa-dev-admins(at)lists.apple.com
 
 Help/Unsubscribe/Update your Subscription:
 https://lists.apple.com/mailman/options/cocoa-dev/zav%40mac.com
 
 This email sent to z...@mac.com

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: set maximum file size

2014-10-08 Thread 2551

 On 8 Oct 2014, at 21:08, Alex Zavatone z...@mac.com wrote:
 
 Why not check the file size before writing? 

Ah, yes, like a kid who picks up the binoculars fat-end first, I was looking at 
things the wrong way around (how to limit the size instead of getting the size 
and doing something with that info).

Thanks a lot, that should do it!


Best


P
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Memory Warnings

2014-10-08 Thread David Duncan
Once your application is in the background it becomes fair game for being 
terminated. So even if you're using a very reasonable mount of RAM, if you're 
in the background you can expect that if some other application needs the 
memory your application will be terminated.

--
David Duncan @ My iPhone

 On Oct 8, 2014, at 2:06 AM, Gerriet M. Denkmann gerr...@mdenkmann.de wrote:
 
 
 On 8 Oct 2014, at 13:42, Quincey Morris 
 quinceymor...@rivergatesoftware.com wrote:
 
 (reposted after editing for length)
 
 On Oct 7, 2014, at 22:12 , Gerriet M. Denkmann gerr...@mdenkmann.de wrote:
 
 Is there a way for the app to find out how much memory it is currently 
 using?
 
 Here’s my take on this, which may be way off base:
 
 There’s really no practical way to answer such questions any more. The way 
 memory is managed is so varied, so diffused that even if you could get an 
 answer, it would be as likely to lead you astray as to help you.
 
 In practice, I think the question is not how much memory your app is using, 
 but how fast your memory use is increasing over time.
 
 If it’s not increasing over time
 It's not. Checked also with Leaks Instrument.
 ― then you have a fairly easy evaluation. Your app fits (in the foreground) 
 or it crashes. Your app retreats gracefully (to the background) or it’s 
 terminated.
 It runs in the foreground. But when in background it sometimes gets 
 terminated.
 
 
 When I have a file of 100 KB and do:
myData = [NSData dataWithContentsOfFile:options:error: ];
myBytes = myData.bytes;
 how much Ram am I now using? 100 KB or 200 KB? I.e. does bytes just 
 return a pointer to some internal structure in NSData?
 
 Would options = NSDataReadingMappedIfSafe be of some help?
 
 The general answer is that you don’t know how much it’s using. In practice, 
 the answer used to be 100KB (“bytes” returned an interior pointer). But this 
 isn’t always true any more. GCD data blocks, which can be bridged to NSData, 
 can come in pieces, and in that case “bytes” may copy the data into 
 contiguous memory.
 
 Mapped data might help in common cases, but how does this save you from “it 
 fits or it crashes” when the file turns out to be on a non-local file 
 system, on which mapping doesn’t work? Memory mapping is more about 
 performance than size, perhaps.
 
 I made a small iOS app which has just 3 buttons: 
 Data (loads NSData from file, which resides in the app bundle)
 Bytes (which uses myData.bytes)
 Count (which sums all bytes in myData)
 
 Result with NSDataReadingMappedIfSafe, Persistent Bytes in Instruments, 
 Allocations:
 No change with any button (always ca. 3.5 MB).
 Data button takes 1 msec, Bytes 73 μsec, first Count takes 250 msec longer 
 than subsequent Counts.
 
 Without NSDataReadingMappedIfSafe:
 Data button increases Ram use by 15 MB (about size of file); takes 250 msec.
 Bytes, Count: no change. Bytes takes 13 μsec. All Counts take same time (ca. 
 50 msec).
 
 So NSDataReadingMappedIfSafe seems to be a good idea (at least in my case of 
 a fixed file in the app bundle).
 
 
 You can easily use a construct like GCD semaphores...
 
 Sounds too much work for now. Maybe to be considered later. Thanks for your 
 ideas though.
 
 
 Kind regards,
 
 Gerriet.
 
 
 ___
 
 Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
 
 Please do not post admin requests or moderator comments to the list.
 Contact the moderators at cocoa-dev-admins(at)lists.apple.com
 
 Help/Unsubscribe/Update your Subscription:
 https://lists.apple.com/mailman/options/cocoa-dev/david.duncan%40apple.com
 
 This email sent to david.dun...@apple.com

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Responsive scrolling: prepareContentInRect: not called

2014-10-08 Thread Kyle Sluder
On Oct 8, 2014, at 2:53 AM, Antonio Nunes devli...@sintraworks.com wrote:
 
 Turns out the problem is a different, and of course, almost embarrassingly 
 silly one. I misspelled the method name: prepareContentInrect rather than 
 prepareContentInRect. Now that I’ve caught that and corrected it, the method 
 is called. Thanks for you effort anyway.

Hmm, this sounds like it would make a great compiler warning. File a Radar, 
maybe?

--Kyle Sluder
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Waiting for a future runloop-based callback

2014-10-08 Thread Jens Alfke
I thought I understood runloops pretty well, but I'm running into a situation 
that's got me stumped. Basically it involves how to wait (block) for a future 
message-send that will be queued onto the current runloop.

So I've started an asynchronous task that runs on a background thread. When the 
task completes, it's going to notify the originating thread via a call to 
-performSelector:onThread:

In some circumstances the program later decides it need to block the 
originating thread until the operation completes. So it runs a nested runloop 
like this:
while (!taskIsComplete)
[[NSRunLoop currentRunLoop] runMode:  @myCustomMode
beforeDate: [NSDate distantFuture]];
where taskIsComplete is some flag that will get set by the target method 
invoked from the task thread.

(I use a custom runloop mode because, if I used the default mode, the 
application would respond to user input and other events while waiting in the 
loop, resulting in re-entrant calls to the application code, causing all sorts 
of problems. In fact what I'm actually doing is fixing a bug in the current 
code where it incorrectly uses the default mode, leading to a crash.)

Over on the task thread, when it completes the task it calls:
[originator performSelector: @selector(taskDone:) onThread: 
originatingThread withObject: self 
waitUntilDone: NO modes: @[@myCustomMode]];
where the -taskDone: method will set that taskIsComplete ivar that the wait 
code is checking.

Unfortunately this doesn't work, because the runloop immediately bails out and 
returns NO from -runMode:beforeDate:. Apparently this is because there are no 
input sources registered for myCustomMode. Which is true, but that doesn't mean 
that no events are going to arrive for that mode! The problem seems to be that 
this part of the runloop implementation doesn't understand about 
delayed-perform-in-mode calls.

What's the workaround? Should I add some sort of no-op input source to the 
runloop for my custom mode? That smells very hacky.

—Jens
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Waiting for a future runloop-based callback

2014-10-08 Thread Ken Thomases
On Oct 8, 2014, at 1:45 PM, Jens Alfke j...@mooseyard.com wrote:

 In some circumstances the program … runs a nested runloop like this:
   while (!taskIsComplete)
   [[NSRunLoop currentRunLoop] runMode:  @myCustomMode
   beforeDate: [NSDate distantFuture]];
 where taskIsComplete is some flag that will get set by the target method 
 invoked from the task thread.

 Over on the task thread, when it completes the task it calls:
   [originator performSelector: @selector(taskDone:) onThread: 
 originatingThread withObject: self 
   waitUntilDone: NO modes: @[@myCustomMode]];
 where the -taskDone: method will set that taskIsComplete ivar that the wait 
 code is checking.
 
 Unfortunately this doesn't work, because the runloop immediately bails out 
 and returns NO from -runMode:beforeDate:. Apparently this is because there 
 are no input sources registered for myCustomMode. Which is true, but that 
 doesn't mean that no events are going to arrive for that mode! The problem 
 seems to be that this part of the runloop implementation doesn't understand 
 about delayed-perform-in-mode calls.

It's not about understanding perform-selector calls.  It's simply that 
there's no input source at the time you're running the run loop. The fact that 
there may be input sources in the future doesn't matter.

 What's the workaround? Should I add some sort of no-op input source to the 
 runloop for my custom mode?

Yes.  Just add an [NSPort port] to the run loop.

 That smells very hacky.

Yeah, but what are you gonna do?

Regards,
Ken


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Waiting for callback with GCD: impossible?

2014-10-08 Thread Jens Alfke
On a related topic from my last post, looking at the equivalent situation but 
using GCD instead of runloops, the problem appears to be impossible. That is:

* Code running on dispatch queue A starts some async task that will run on 
another queue B, and which will signal completion by using dispatch_async to 
call a completion block on queue A, passing it some result.

* After the task starts, the logic on queue A decides it needs to block until 
the task finishes and returns its result. (This might be part of an 
implementation of the 'future' design pattern, for example.)

* Now, how does queue A wait for the callback? It seems to be impossible: the 
only way for code running on queue A to receive the callback is to return back 
to the queue dispatcher, so if it tries to wait it simply blocks the queue, 
causing a deadlock.

This is exactly why runloops have the ability to run a nested loop, to block 
but still allow incoming messages. Of course that comes with its own set of 
complications, like the necessity for multiple modes, and issues like the one 
I just brought up.

But given that dispatch queues can't do this, what's the solution to this 
design problem, i.e. turning an async call into a sync one?

—Jens
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Waiting for callback with GCD: impossible?

2014-10-08 Thread davekeck .
 * Now, how does queue A wait for the callback? It seems to be impossible: the 
 only way for code running on queue A to receive the callback is to return 
 back to the queue dispatcher, so if it tries to wait it simply blocks the 
 queue, causing a deadlock.

You could use some other signaling mechanism -- a dispatch_semaphore_t
or NSConditionLock, for example.
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Waiting for callback with GCD: impossible?

2014-10-08 Thread Seth Willits
On Oct 8, 2014, at 11:59 AM, Jens Alfke j...@mooseyard.com wrote:

 * Now, how does queue A wait for the callback? It seems to be impossible: the 
 only way for code running on queue A to receive the callback is to return 
 back to the queue dispatcher, so if it tries to wait it simply blocks the 
 queue, causing a deadlock.
 …
 But given that dispatch queues can't do this, what's the solution to this 
 design problem, i.e. turning an async call into a sync one?

Simply performing the rest of the task in the callback is one way.


--
Seth Willits




___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Waiting for callback with GCD: impossible?

2014-10-08 Thread Ken Thomases
On Oct 8, 2014, at 1:59 PM, Jens Alfke j...@mooseyard.com wrote:

 * Code running on dispatch queue A starts some async task that will run on 
 another queue B, and which will signal completion by using dispatch_async to 
 call a completion block on queue A, passing it some result.
 
 * After the task starts, the logic on queue A decides it needs to block until 
 the task finishes and returns its result. (This might be part of an 
 implementation of the 'future' design pattern, for example.)

Queues are not like run loops nor threads.  Your desire to have ongoing logic 
occur on a particular queue seems odd to me.  The main purpose for custom 
queues would be to synchronize access to specific resources.  Why is there 
logic on queue A that's doing anything other than briefly accessing a shared 
resource?

For what it's worth, GCD isn't a good fit for the future design pattern.  The 
GCD way of doing this is to have a task that will be executed asynchronously 
at the time the result becomes available.  The A logic shouldn't wait; it 
should arrange for whatever work it would do when the wait is over to happen at 
that time.  Then it should end.

 * Now, how does queue A wait for the callback? It seems to be impossible: the 
 only way for code running on queue A to receive the callback is to return 
 back to the queue dispatcher, so if it tries to wait it simply blocks the 
 queue, causing a deadlock.

Yup.

 But given that dispatch queues can't do this, what's the solution to this 
 design problem, i.e. turning an async call into a sync one?

Avoid it.  The desire to turn async work to synchronous work is the root of the 
problem.

That said, the task on queue B could signal a semaphore or leave a group, 
rather than attempting to submit a task to queue A.  If you must block waiting 
for the result, the A logic could wait on the semaphore or group.  Better would 
be to use dispatch_group_notify() to just perform the when-the-result-is-ready 
work when it's ready.

Regards,
Ken


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

UIWebView not scaling page (iOS8)

2014-10-08 Thread Diederik Meijer | Ten Horses
Hi list,

I have a UIWebView inside a UITableViewCell that simply won’t scale the page to 
fit on iOS8. I have set it to scale pages to fit in IB and have also tried 
setting it in code, but it simply won’t do it.

The UIWebView depicts a JPG image located at a web location.

I had no trouble with this on iOS7.

It may be bad practise to stuff a web view inside a tableViewCell, I am not 
sure. But it should still be able to scale the page to fit into it, shouldn’t 
it?


Many thanks,


Diederik
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Waiting for callback with GCD: impossible?

2014-10-08 Thread Jens Alfke

 On Oct 8, 2014, at 12:06 PM, davekeck . davek...@gmail.com wrote:
 
 You could use some other signaling mechanism -- a dispatch_semaphore_t
 or NSConditionLock, for example.

Yeah, but that doesn't work for the async mode, where I need a dispatch_async 
call to queue A to notify it.
Maybe I'd also need a shared flag that indicates async vs. sync mode, and queue 
B would look at the flag and either dispatch a block or toggle the condition 
based on its state.

—Jens
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Creating a Photo Extension

2014-10-08 Thread Daniel Blakemore
Nothing leaves you feeling like a stroke victim more than doing something
you know you've done before and having it fail all over the place.

I'm trying to create a simple application with a dummy photo extension
written in swift.  I have already written an actual application with a
photo extension and that works (as well as it can given the occasional
nondeterministic behavior we and others have been seeing).

To make this demo app, made a new single-view application in Xcode.

Then I add a photo extension target, making sure to select Swift as the
language.

Then I make sure to go tell the application target that Embedded content
contains swift code because otherwise the photo extension will fail at
runtime with the *wildly misleading* and unhelpful error message: blah
blah blah swiftrelated.dylib blah:image not found (not, in fact, referring
to the image the photo extension has been asked to load).

Then I delete the Main.storyboard because I don't like storyboards.

Then I edit the info.plist created for the extension to remove the
NSExtensionMainStoryboard key and replace it with an
NSExtensionPrincipalClass key.

Then I run the photo extension target in the Photos app.

Upon selecting a photo and attempting to edit it with my new extension, I
get this error:

Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '*** setObjectForKey: object cannot be nil (key: __NSConcreteUUID
0x7ffdd3f12c60 21074A67-F7EF-423D-A245-8805C0CC134F)'
*** First throw call stack:
(
0   CoreFoundation  0x000109d843f5
__exceptionPreprocess + 165
1   libobjc.A.dylib 0x00010b8f1bb7
objc_exception_throw + 45
2   CoreFoundation  0x000109c8af18
-[__NSDictionaryM setObject:forKey:] + 968
3   libextension.dylib  0x00010b8b217a
-[_NSExtensionContextVendor _setPrincipalObject:forUUID:] + 114
4   libextension.dylib  0x00010b8b1880
__105-[_NSExtensionContextVendor
_beginRequestWithExtensionItems:listenerEndpoint:withContextUUID:completion:]_block_invoke
+ 822
5   libdispatch.dylib   0x00010cdd0c06
_dispatch_call_block_and_release + 12
6   libdispatch.dylib   0x00010cdeaaf4
_dispatch_client_callout + 8
7   libdispatch.dylib   0x00010cdd62e9
_dispatch_main_queue_callback_4CF + 490
8   CoreFoundation  0x000109cec569
__CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
9   CoreFoundation  0x000109caf46b __CFRunLoopRun +
2043
10  CoreFoundation  0x000109caea06
CFRunLoopRunSpecific + 470
11  GraphicsServices0x00011008a9f0 GSEventRunModal
+ 161
12  UIKit   0x00010a60e550
UIApplicationMain + 1282
13  libxpc.dylib0x00010d0e3b4b _xpc_objc_main +
416
14  libxpc.dylib0x00010d0e5ee0 xpc_main + 185
15  Foundation  0x00010a3311d1
service_connection_handler + 0
16  PlugInKit   0x00010960fba2 -[PKService run]
+ 521
17  PlugInKit   0x00010960f867 +[PKService
main] + 55
18  PlugInKit   0x00010960fbc6 +[PKService
_defaultRun:arguments:] + 17
19  libextension.dylib  0x00010b8bfcdd NSExtensionMain
+ 51
20  libdyld.dylib   0x00010ce1b145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException


Well, the ONLY reference to this that I can find is that some framework
isn't being implicitly loaded and is dying here.
See http://www.atomicbird.com/blog/ios-app-extension-tips at the section
titled Enabling modules might not work as expected

I have literally been pulling my hair out for two hours because I can't get
my boilerplate to work and I was actually trying to make a useful demo
project, not just bash my head against the same wall (not to mention the
hour that I spent trying to get Xcode to NOT build swift successfully when
it had copy-pasted Objective-C in it that was clearly not correct [which
required me to restart my entire computer]).

What am I doing wrong that I need to do right?

--
Daniel Blakemore
Pixio Software
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Waiting for callback with GCD: impossible?

2014-10-08 Thread Jens Alfke

 On Oct 8, 2014, at 12:28 PM, Ken Thomases k...@codeweavers.com wrote:
 
 Queues are not like run loops nor threads.  Your desire to have ongoing logic 
 occur on a particular queue seems odd to me.

I don't think it's odd to want an ongoing single flow of control for specific 
parts of the program. Limiting parallelism makes it a lot easier to reason 
about the code's behavior. You can create one of those with a background 
thread, but my understanding is that we're encouraged to use serial dispatch 
queues instead. I've certainly done this with threads in the past, but there's 
complexity with setting up the thread's main method that runs a runloop and 
knows how to exit when it's done; plus, newer system APIs seem to be moving 
away from runloops (e.g. NSURLSession.)

This is part of why I still find the runloop-vs-queue-vs-NSOperation issue such 
a headache. Apple's given us a lot of concurrency options, and it's really 
unclear which ones to use when. Especially when a lot of the Cocoa classes work 
with only some of them but not others. (For example NSStream only supports 
runloop scheduling while NSURLSession only supports NSOperationQueues.)

  The main purpose for custom queues would be to synchronize access to 
 specific resources.  Why is there logic on queue A that's doing anything 
 other than briefly accessing a shared resource?

I disagree about that, actually. The advice to wrap critical sections in 
dispatch_async calls turns out to produce a _lot_ of overhead. I briefly used 
it in some performance-sensitive code this spring, but had to rip it out 
because a large fraction of runtime was being spent in OS code that scheduled 
dispatch queues, copied/freed blocks, juggled retain counts, etc. It was a lot 
faster to just use @synchronized, and faster still to make the classes in 
question single-threaded and push the synchronization tasks up to a higher 
level of the code. (This isn't just obsessing over clock cycles; I'm literally 
talking about 2x performance improvements in high-level benchmarks.)

—Jens
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Get text with hyphenation

2014-10-08 Thread Leonardo
Hi, I have to draw several lines of text, one by one, to an HTML page.
I get the text from an NSTextView, iterating throught the text lines:

lineRect = [layoutManager lineFragmentRectForGlyphAtIndex:index
effectiveRange:lineGlyphRange];
index = NSMaxRange(lineGlyphRange);
lineTextRange = [layoutManager
characterRangeForGlyphRange:lineGlyphRange
actualGlyphRange:actualTextRange];
textLine = [self.textStorage attributedSubstringFromRange:lineTextRange]

It works, but textLine doens't contain the hyphenation dash, if visible in
the textView. For example, if the text line ends with last word of the li-
I just get last word of the li.

I have found a dirty workaround

if(lineGlyphRange.length  lineTextRange.length)
{
// I manually add a dash - at the end of the textLine extracted.
}

but I guess it will not work in many other cases, e.g. when the glyphs are
more than the chars because of some umlaut...
How to get that dash, if any? Thank you.


Regards
-- Leonardo


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Waiting for callback with GCD: impossible?

2014-10-08 Thread Ken Thomases
On Oct 8, 2014, at 3:15 PM, Jens Alfke j...@mooseyard.com wrote:

 On Oct 8, 2014, at 12:28 PM, Ken Thomases k...@codeweavers.com wrote:
 
 Queues are not like run loops nor threads.  Your desire to have ongoing 
 logic occur on a particular queue seems odd to me.
 
 I don't think it's odd to want an ongoing single flow of control for specific 
 parts of the program. Limiting parallelism makes it a lot easier to reason 
 about the code's behavior.

We're not talking about parallelism here, at least not any more so than you 
already had by having A and B.  We're talking about (a)synchronicity, which is 
a separate thing.

For example, this is still serial execution (of this local logic) and a single 
flow of control:

mainThreadTask1();
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 
0), ^{
backgroundTask1();
dispatch_async(dispatch_get_main_queue(), ^{
mainThreadTask2();

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
backgroundTask2();
dispatch_async(dispatch_get_main_queue(), ^{
mainThreadTask3();
// etc.
});
});
});
});

 You can create one of those with a background thread, but my understanding is 
 that we're encouraged to use serial dispatch queues instead.

I don't know where you got that impression, but that seems like a terrible idea 
to me.  First of all, a queue is just a data structure.  A queue doesn't run 
anything, itself.  It is still serviced by threads.  If you're not using the 
data management features of the queue — its width, suspending/resuming, barrier 
tasks, etc. — there's little point.

Submitting long-running tasks to a global concurrent queue is a perfectly 
acceptable way to get them to run on a background thread.  Some people object, 
but I have no problem with it.  It doesn't have any particular advantage over 
spawning a thread, though.

But using a custom queue for that, just to avoid creating a thread explicitly, 
seems pointless to me.  And I especially don't see the point in using a serial 
queue when you don't need to synchronize.


  The main purpose for custom queues would be to synchronize access to 
 specific resources.  Why is there logic on queue A that's doing anything 
 other than briefly accessing a shared resource?
 
 I disagree about that, actually. The advice to wrap critical sections in 
 dispatch_async calls turns out to produce a _lot_ of overhead. I briefly used 
 it in some performance-sensitive code this spring, but had to rip it out 
 because a large fraction of runtime was being spent in OS code that scheduled 
 dispatch queues, copied/freed blocks, juggled retain counts, etc. It was a 
 lot faster to just use @synchronized, and faster still to make the classes in 
 question single-threaded and push the synchronization tasks up to a higher 
 level of the code. (This isn't just obsessing over clock cycles; I'm 
 literally talking about 2x performance improvements in high-level benchmarks.)

That doesn't seem like a disagreement to me.  I said that's the purpose for 
custom queues.  You say they aren't well suited for the purpose.  That doesn't 
change the fact that that's their purpose.  If you find them poorly suited, 
then that's all the stronger argument not to use them.

Regards,
Ken

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Table of TextViews

2014-10-08 Thread Charles Jenkins
I’m still struggling to create a single scrollview containing a stack of 
textviews.  

I created an object called “Subdocument” which has two important readonly 
properties:

NSTextView* myView
CGFloat myHeight

myHeight returns myView's frame hight, but never less than 10. The 
Subdocument’s initializers take care of instantiating and connecting up all 
four text objects (TextView, TextContainer, LayoutManager, and TextStorage).

For now, my Document class has one important property which is read/write:

NSMutableArray* subdocumentStack;

The Document initializer creates and plugs in an array of 20 subdocuments as a 
test.

In my NIB, I add an NSArrayController and set it to manage the Subdocument 
class and bind it to File’s Owner’s subdocumentStack.

Then I add a view-based NSTableView and turn off unnecessary things like column 
headers, column selection, and column resizing.

If I run my application at this point, everything is fine, but the table is 
empty. NSLog statements confirm the subdocumentStack contains 20 objects with 
views and heights.

Now I try to bind the table. I select the Table View object and bind Table 
Content to the array controller’s arrangedObjects myView key path. Then I bind 
its Row Height to the array controller’s arrangedObjects myHeight key path.

The next time I run the program, no document ever appears. Initialization 
happens normally, but before the window opens, an error is logged: [__NSArrayI 
doubleValue]: unrecognized selector sent to instance address.

If I unbind the Row Height, the document window will appear, but the views are 
all copies of the default Text View Cell.

Now, IB liked all the model key paths I chose. subdocumentStack was in the 
suggestion list for the array controller; and myView and myHeight were in the 
suggestion lists for table bindings.

So here’s my first question:

What if anything am I doing wrong when I try to bind the row height? Adding 
that one thing causes the error and interrupts the NIB from loading.

—

Charles Jenkins

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Table of TextViews

2014-10-08 Thread Ken Thomases
On Oct 8, 2014, at 8:59 PM, Charles Jenkins cejw...@gmail.com wrote:

 Now I try to bind the table. I select the Table View object and bind Table 
 Content to the array controller’s arrangedObjects myView key path. Then I 
 bind its Row Height to the array controller’s arrangedObjects myHeight key 
 path.
 
 The next time I run the program, no document ever appears. Initialization 
 happens normally, but before the window opens, an error is logged: 
 [__NSArrayI doubleValue]: unrecognized selector sent to instance address.

 What if anything am I doing wrong when I try to bind the row height? Adding 
 that one thing causes the error and interrupts the NIB from loading.

The table view's rowHeight binding is not a per-row row height.  It's a single 
number for the height of all rows of the table.

Using KVC to get the value from the array controller with the key path 
arrangedObjects.myHeight returns an array.  -valueForKey: applied to an array 
will always produce an array, and -valueForKeyPath: is just a series of 
-valueForKey: calls.  Then, the table view calls -doubleValue on whatever 
object KVC obtained.  Hence the error that an array class doesn't respond to 
-doubleValue.

If you want separate row heights for each row, you need to implement a table 
view delegate and the method -tableView:heightOfRow:.  You would look up the 
object for the row by indexing into the array controller's arrangedObjects and 
obtain the height from your myHeight property.

If/when you change the height of a subdocument, you need to inform the table 
view by calling -noteHeightOfRowsWithIndexesChanged: on it.

Regards,
Ken


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com